So I am getting Forbidden (403) CSRF verification failed. Request aborted. Reason given for failure: CSRF token missing or incorrect.
I have the 'django.middleware.csrf.CsrfViewMiddleware', in my middleware_classes.
Here is my template
<form name="input" action="/login/" method="Post"> {% csrf_token %}
<input type="submit" value="Submit"></form>
Here is my view
from django.shortcuts import render_to_response
from django.core.context_processors import csrf
from django.template import RequestContext
def login(request):
csrfContext = RequestContext(request)
return render_to_response('foo.html', csrfContext)
Well I am new to Django and most web development, but I cannot seem to find the problem here. Any help would be much appreciated!
Also i have tried the method in the django documentation
c = {}
c.update(csrf(request))
# ... view code here
return render_to_response("a_template.html", c)
You should do the following:
Here are official docs for
render_to_response
.go urls and add the .as_view() next to the view metho/class ex.
ObtainAuthTokenView.as_view()
I had the same problem with you and i found this code that solve my problem.
Just add this in your HTML:
Try adding the @csrf_protect decorator just before your login function.
If the form is not in foo.html then you need to add the @csrf_protect method to the view function that is generating it.