2012-05-12 71 views
0

我得到當我點擊提交我的虛擬形式Django的形式禁止(403)錯誤

禁止(403)

CSRF驗證失敗的按鈕下面的錯誤。請求中止

我views.py(做了以上所需的進口)是這樣的:

def search(request): 
     errors=[] 
     if request.method=="POST": 
      if not request.POST['name']: 
         errors.append('Name field is empty') 
      if not request.POST['subject']: 
         errors.append('Subject field is empty') 
      if not request.POST['age']: 
         errors.append('Age field is empty') 
      if not errors: 
         thank() 
     return render_to_response('search.html',{'errors':errors},context_instance=RequestContext(request)) 

def thank(search): 
     return HttpResponse('<html><p>Post done successfully</p></html>') 

我search.html是:

<form method="post" action='/search/'> 
    <p>Name: <input type="text" name="name"/></p> 
    <p>Subject <input type="text" name="subject"/></p> 
    <p>Age: <input type="text" name="age"/></p> 
    <input type="submit" value="Hit Me!!"/> 
    </form> 
    </body> 
</html> 

有人請讓我知道,我如何克服這個錯誤?

回答

0

那麼,

1。在settings.py中將'django.core.context_processors.csrf'添加到TEMPLATE_CONTEXT_PROCESSORS設置。 2.修改您的形式是這樣,

<form method="post" action='/search/'> 
    {% csrf_token %} 
    <p>Name: <input type="text" name="name"/></p> 
    <p>Subject <input type="text" name="subject"/></p> 
    <p>Age: <input type="text" name="age"/></p> 
    <input type="submit" value="Hit Me!!"/> 
</form> 
0

我想說,我沒有看到你的<form></form>標籤之間的{% csrf_token %}。這將導致CSRF驗證失敗。上面的海報打敗了我。