python - Django csrf token missing or incorrect error 403 -
when submit form following error:
csrf verification failed
reason given failure:
csrf token missing or incorrect.
my views.py is:
def name(request): if request.method == 'post': form=nameform(request.post) if form.is_valid(): name=form.cleandata['your_name'] return httpresponseredirect('/thanks/',requestcontext(request)) else: form=nameform() return render_to_response('contact.html')
my setting.py file:
middleware = [ 'django.middleware.security.securitymiddleware', 'django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', 'django.middleware.clickjacking.xframeoptionsmiddleware', ]
my forms.py file is:
class nameform(forms.form): your_name=forms.charfield(initial='your name',max_length=100)
my contact.html is:
<form action="/your-name/" method="post"> {% csrf_token %} {{form}} <input type="submit" value="submit" /> </form>
urls.py is:
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^search/$', search),url(r'^contact/$',contact), url(r'^name/$',name),url(r'^your-name',name),url(r'^thanks/$',thank) ]
use render
function render template, instead of render_to_response
.
from django.shortcuts import render def name(request): if request.method == 'post': form = nameform(request.post) if form.is_valid(): name = form.cleaned_data['your_name'] return httpresponseredirect('/thanks/', requestcontext(request)) else: form = nameform() return render(request, 'contact.html')
Comments
Post a Comment