javascript中呈现的表单不接受Csrf令牌



我正试图通过django中的javascript来呈现一个表单。我试着通过栈溢出文章中描述的方式插入csrf令牌标记。我如何包含Django 1.2';在Javascript生成的HTML表单中的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>

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

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 + "'/>";
}
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 + "'/>";
}

最新更新