0

我正在尝试通过 django 中的 javascript 呈现表单。我尝试通过堆栈溢出帖子中描述的方式插入 csrf 令牌标记如何在 Javascript 生成的 HTML 表单中包含 Django 1.2 的 CSRF 令牌?但这对我不起作用:/我已经尝试了几个小时来找出原因!

var CSRF_TOKEN = document.getElementById('csrf_token').value;

for (i = 0; i < this.ShoeList.length; i++) {
html += "<form method='POST' action='/AddToCart/'>'"+CSRF_TOKEN+"'<div class = 'box'<li><img src=' " + this.ShoeList[i].imageSource + "'/>";
}

这是我的模板代码:

<body onload="start()">
    <input id="csrf_token" value="{{ csrf_token }}"/>
</body>

当我运行代码时,令牌值正在正确显示。但由于某种原因它不接受令牌。请帮忙!

4

2 回答 2

0
for (i = 0; i < this.ShoeList.length; i++) {
html += "<form method='POST' action='/AddToCart/'>{% csrf_token %}<div class = 'box'<li><img src=' " + this.ShoeList[i].imageSource + "'/>";
}
于 2014-05-01T12:23:25.683 回答
0
var CSRF_TOKEN = document.getElementById('csrf_token').value;

for (i = 0; i < this.ShoeList.length; i++) {
html += "<form method='POST' action='/AddToCart/'><input type='hidden' value='"+CSRF_TOKEN+"' name='csrfmiddlewaretoken'><div class = 'box'<li><img src=' " + this.ShoeList[i].imageSource + "'/>";
}
于 2014-05-01T13:20:06.860 回答