【发布时间】: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