Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

429
Views
Django csrf token missing or incorrect error 403

When i submit the form i get following error:

CSRF verification failed

Reason given for 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)
]
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Use the render function to render the template, instead of the 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')
about 4 years ago · Santiago Trujillo Report

0

Use @csrf_protect decorator. You can get details about csrf here

about 4 years ago · Santiago Trujillo Report

0

csrf Forbidden (CSRF token missing or incorrect.) when submitting a request:

In a form, include {% csrf_token %}, which generates an input tag with the csrf token value, and in the request, have the headers include X-CSRFTOKEN

headers: {
  content_type: 'application/json',
  'X-CSRFToken': "{{ csrf_token }}"
},

https://docs.djangoproject.com/en/3.1/ref/csrf/

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!