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

时间:2018-04-18 04:44:54

标签: html ajax django django-templates

我一直在嘲笑这个问题太久了,因为我正在苦心地引入ajax来加速我的django应用程序。我没有以前的经验。我有一个下拉列表,我将其用作通知查看器并使用{%for loop%}来填充。他们都拥有相同的身份但唯一的名字 - 他们的记录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">

请协助。

1 个答案:

答案 0 :(得分:2)

<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>