如何将值从 django 模板化 html 传递到 ajax

Posted

技术标签:

【中文标题】如何将值从 django 模板化 html 传递到 ajax【英文标题】:How to pass value from django templated html to ajax 【发布时间】:2018-09-28 04:50:52 【问题描述】:

我一直在研究这个问题太久了,因为我煞费苦心地引入 ajax 来加速我的 django 应用程序。我没有以前的经验所以。我有一个下拉列表,用作通知查看器并使用 %for loop% 进行填充。它们都共享相同的 id 但唯一的名称——它们的记录 id。我正在尝试单击通知,然后通过将其 ID 传递给我的 views.py 文件来加载相应的记录。以下是我的骇客尝试的代码,该尝试未能取得成果并花费了很长时间。

<script>
    function openNotification()
        $('#ntfy').click(function(e) 
            var acc = $(this).attr('name');
            $.ajax(
                type: "GET",
                url: "% url 'my_app:notifications' %",
                dataType: "json",
                async: true,
                data:csrfmiddlewaretoken :' csrf_token ',
                     'acc':acc,
                success: function()
                    alert("yo yoyo");
                    if (data.status == "success")
                        window.location.href = data.url;
                    
                    else
                        alert("error occured");
                    
                
            );
        );
    

</script>

html 看起来像这样。

<a href="#" onclick="openNotification()" name="alert.1.docnum_alert.0.accountid" id="#ntfy">

请帮忙。

【问题讨论】:

acc 是什么?您是否尝试以alert.1.docnumalert.0.accountid 访问值? 抱歉,打错了。我已经编辑以显示我的意图。我希望将这两个作为一个值传递,因为通知是复合主键 了解,请查看我对 2 种不同方法的回答。应该工作。 回调函数缺少data 变量,所以我补充说现在请你也修复它。 谢谢!奇迹般有效。选择了第二个版本。 【参考方案1】:
<a href="#" data-docnum="alert.1.docnum" data-accountid="alert.0.accountid" class="ntfy">

=============================================

<script>
    $('.ntfy').on('click',function(e) 
        var docnum = $(this).attr('data-docnum'); // or $(this).data('docnum')
        var accountid = $(this).attr('data-accountid'); // or $(this).data('accountid')
        data = 
            "csrfmiddlewaretoken":$("input[name='csrfmiddlewaretoken']").val(),
            "docnum":docnum,
            "accountid":accountid
        
        $.ajax(
            type: "GET",
            url: "% url 'rznbldbt_app:notifications' %",
            dataType: "json",
            async: true,
            data:data,
            success: function(data)
                alert("yo yoyo");
                if (data.status == "success")
                    window.location.href = data.url;
                
                else
                    alert("error occured");
                
            
        );
    );
</script> 

另一种方法是:

<a href="#" onclick="openNotification(alert.1.docnum,alert.0.accountid);">

=============================================

<script>    
    function openNotification(docnum,accountid)
    
        data = 
            "csrfmiddlewaretoken":$("input[name='csrfmiddlewaretoken']").val(),
            "docnum":docnum,
            "accountid":accountid
        
        $.ajax(
            type: "GET",
            url: "% url 'rznbldbt_app:notifications' %",
            dataType: "json",
            async: true,
            data:data,
            success: function(data)
                alert("yo yoyo");
                if (data.status == "success")
                    window.location.href = data.url;
                
                else
                    alert("error occured");
                
            
        );
    
</script>

【讨论】:

以上是关于如何将值从 django 模板化 html 传递到 ajax的主要内容,如果未能解决你的问题,请参考以下文章

如何将值从 javascript 函数传递到 django 视图

将值从 fullCalendar 单击传递到 Django 模板

将值从 Django 视图传递到 AngularJS 插入的模板

如何将值从 html 传递到 django 中查看?

如何通过 ajax 将值从 django 模板发送到视图?

如何将值从模板传递到 ts 函数