0
votes

I am getting the below error when i click on submit button of my dummy form

Forbidden (403)

CSRF verification failed. Request aborted

My views.py(have done the required imports above) looks like this:

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>')

My search.html is :

    <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>

Someone please let me know,how can i overcome this error?

2

2 Answers

0
votes

Well,

1 . Add 'django.core.context_processors.csrf' to TEMPLATE_CONTEXT_PROCESSORS settings in settings.py. 2. Modify your forms like this,

<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
votes

I was going to say, I don't see a {% csrf_token %} between your <form></form> tags. That will cause CSRF verification to fail. The above poster beat me to it.