【问题标题】:How to pass value from django templated html to ajax如何将值从 django 模板化 html 传递到 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.docnum}}{{alert.0.accountid}} 访问值?
  • 抱歉,打错了。我已经编辑以显示我的意图。我希望将这两个作为一个值传递,因为通知是复合主键
  • 了解,请查看我对 2 种不同方法的回答。应该工作。
  • 回调函数缺少data 变量,所以我补充说现在请你也修复它。
  • 谢谢!奇迹般有效。选择了第二个版本。

标签: html ajax django django-templates


【解决方案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>

【讨论】:

    猜你喜欢
    • 2013-11-27
    • 2020-06-04
    • 2015-07-07
    • 2018-02-10
    • 2021-06-30
    • 2021-08-26
    • 2015-11-13
    • 2012-10-09
    • 2018-09-03
    相关资源
    最近更新 更多