2014-05-01 20 views
0

我想通過Django中的javascript呈現表單。我試圖通過堆棧溢出後描述的方式插入csrf標記標記How do I include Django 1.2's CSRF token in a Javascript-generated HTML form?,但它不適用於我:/我一直在嘗試幾個小時來找出原因!Csrf令牌不接受在javascript中呈現的表單

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

已經嘗試過。它只在我的html頁面上打印「{%csrf_token%}」。 – user3592590

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