我试图通过django中的javascript渲染表单。我尝试通过堆栈溢出帖How do I include Django 1.2's CSRF token in a Javascript-generated HTML form?中描述的方式插入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>
运行代码时正确显示令牌值。但由于某种原因,它不接受令牌。请帮忙!
答案 0 :(得分: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 + "'/>";
}
答案 1 :(得分: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 + "'/>";
}