Reputation: 2102
I am new to django and have made a method:GET form with checkboxes/text boxes and a submit button. I want to change it to a method:POST form but keep getting the error that says:
Reason given for failure:
CSRF token missing or incorrect.
In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
Your browser is accepting cookies.
The view function uses RequestContext for the template, instead of Context.
In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
I tried to change all the things that the debuger says to but I keep getting the same error. I would like to know if there are any other files that I need to change other than views.py, the template file(.html) and the settings.py file which are included here:
views.py:
from django.template import RequestContext, loader
from crunchApp.models import Filter
from django.http import HttpResponse
from django.core.context_processors import csrf
from django.shortcuts import render_to_response, get_object_or_404
from django.http import Http404
def results(request):
c = {}
c.update(csrf(request))
return render_to_response('crunchApp/results.html', c)
template/test.html
<form name="myform" action="results" method = "post" >{% csrf_token %}
<fieldset>
<input type="checkbox" value="total_money" id = "money_check" name="check" /> Filter by Total Money</br>
</fieldset></form>
settings.py
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
Upvotes: 0
Views: 701
Reputation: 25489
You shouldn't need
c = {}
c.update(csrf(request))
but you do need context_instance=RequestContext(request), as in
render_to_response(<template>, <vars>, context_instance=RequestContext(request))
Upvotes: 0