Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

430
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

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

about 4 years ago · Santiago Trujillo Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda