【问题标题】:How to pass query sets from html to views.py in django using AJAX?如何使用 AJAX 将查询集从 html 传递到 django 中的views.py?
【发布时间】:2020-12-24 00:17:24
【问题描述】:

我是 Django 新手。所以在这里,我试图将两个查询集从 Django html 模板传递给 Django views.py。这必须在单击按钮时发生,因此,我使用 onClick 函数将参数传递给 Ajax,然后从那里将其发送到视图。在这里,与两个查询集一起,我传递了另外两个名为 rid 和 category 的值,它工作正常,但是查询集正在转换为字符串,因此我无法在将查询集传递给views.py 页面。

这是我的views.py 代码:

if request.is_ajax():
    rid = request.POST.get('rid')
    category = request.POST.get('category') 
    extracted_records=request.POST.get('extracted_records')
    show_cards_records=request.POST.get('show_cards_records') 
    
    return render(request,'cards_update.html',{'extracted_records':extracted_records,'show_cards_records':show_cards_records,'rid':rid})

return render(request,'cardpage.html',{'extracted_records':extracted_records,'show_cards_records':show_cards_records,'rid':'1234'})

这是我的主要 html 代码:

<!-- templates/base.html -->
{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title>CardGame</title>
        <link rel="stylesheet" href="/static/css/cardpage.css"}>
    </head>
    <body>
        <center>
            <h1>{{rid}}</h1>
            <div id='update_cards'>

            {% include 'cards_update.html' %}
            </div>
        </center>
    </body>

    <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" type = "text/javascript" charset = "utf-8"></script>
    <script>
        function func(rid,category,extracted_records,show_cards_records) {
            
            $.ajax({
                type: 'POST',
                url: '/Cardpage',
                data:{
                    'rid':rid,
                    'category':category,
                    'extracted_records':extracted_records,
                    'show_cards_records':show_cards_records,
                    
                },

                success: function (response) {
              
                    $("#update_cards").replaceWith(response);
                    alert("card casted.");
                }
            });
        }
    </script>
</html>

这是我的页面html代码:

<center>
    <button onclick="func('{{entry.rid}}','Rewards_And_Stipends','{{extracted_records}}','{{show_cards_records}}')"  type="button" class="submit-btn">REWARDS : {{entry.Rewards_And_Stipends}}</button>  
    <button onclick="func('{{entry.rid}}','Questions_Posted','{{extracted_records}}','{{show_cards_records}}')"  type="button" class="submit-btn">QUESTIONS POSTED : {{entry.Questions_Posted}}</button>
    <button onclick="func('{{entry.rid}}','Questions_Answered','{{extracted_records}}','{{show_cards_records}}')" type="button" class="submit-btn">QUESTIONS ANSWERED : {{entry.Questions_Answered}}</button>
    <button onclick="func('{{entry.rid}}','Quizzes_Played','{{extracted_records}}','{{show_cards_records}}')"  type="button" class="submit-btn">QUIZZES PLAYED : {{entry.Quizzes_Played}}</button>
    <button onclick="func('{{entry.rid}}','Mentorship','{{extracted_records}}','{{show_cards_records}}')"  type="button" class="submit-btn">MENTORSHIP : {{entry.Mentorship}}</button>
</center>

【问题讨论】:

    标签: javascript python html jquery django


    【解决方案1】:

    你应该在ajax的情况下返回HttpResponse,然后在js处处理成功。

    if request.is_ajax():
        rid = request.POST.get('rid')
        category = request.POST.get('category') 
        extracted_records=request.POST.get('extracted_records')
        show_cards_records=request.POST.get('show_cards_records') 
    
        return HttpResponse(json.dumps({'extracted_records':extracted_records,'show_cards_records':show_cards_records,'rid':rid}), content_type='application/json')
    
    return render(request,'cardpage.html',{'extracted_records':extracted_records,'show_cards_records':show_cards_records,'rid':'1234'})
    

    【讨论】:

    • 感谢您的回复,但我无法将查询集从 html 发送到视图而不是从视图发送到 html
    • 所以你应该检查你的网址。
    猜你喜欢
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2017-07-11
    • 2020-02-18
    • 2018-09-28
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多