i am trying to do a login in django but i get this error, i check the CSRF documentation and nothing works for me.
Here is the HTML:
<body>
<section class="container">
<div class="login">
<h1>Login to Web App</h1>
{% if form.errors %}
<p class="error">Lo sentimos, la combinacion de usuario y contrasena no es correcta!</p>
{% endif %}
<form action="/accounts/auth/" method="post">
{% csrf_token %}
<input type='hidden' name='csrfmiddlewaretoken' value='randomchars'/>
<p><input name="username" type="text" name="login" value="" placeholder="Username"></p>
<p><input name="password" type="password" name="password" value="" placeholder="Password"></p>
<p class="submit"><input type="submit" name="commit" value="Login"></p>
</form>
</div>
</body>
Like you see above i use the {% csrf_token %} and i have 'django.middleware.csrf.CsrfViewMiddleware' in my installed apps.
And my views are:
from django.http import HttpResponse,HttpResponseRedirect
from django.template.loader import get_template
from django.template import Context
from datetime import datetime
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.core.context_processors import csrf
from models import *
from django.shortcuts import get_object_or_404
from forms import *
from django.template.context import RequestContext
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login
def login(request):
c = {}
c.update(csrf(request))
return render_to_response('login.html', c)
def auth_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
auth.login(request.user)
return HttpResponse('/accounts/loggedin')
else:
return HttpResponse('/accounts/invalid')
i redirect to an other HTML file where i dont use the {% csrf_token %}.
Clear your browser cache and try again. Maybe it is using the CSRF token saved in cached cookie.
Theory
A couple of things are required to make the csrf protection work (check out the docs):
django.middleware.csrf.CsrfViewMiddleware'
included as middleware in yoursettings.py
(alternatively use the decorator csrf_protect() on particular views you want to protect)django.core.context_processors.csrf
to the context manager.When you load your page, have a look in the page source using your favorite browser. Don't open the template html file, open the url which point to the view containing the form. Look at where you placed the
{% csrf_token %}
. If you see something likeyou should be ok.
If you on the other hand see
NOTPROVIDED
, something has gone wrong while creating the csrf token. By looking in the source code (context_processors.py
andcsrf.py
), we can find out what:csrf(request)
returns{'csrf_token': 'NOTPROVIDED'}
ifget_token(request)
returns None.get_token(request)
returnsrequest.META.get("CSRF_COOKIE", None)
.I assume this means that it would return
None
if the cookie isn't successfully created.Fix
For you, this means that you should first replace
with
We'd like the csrf field to be inside
<form>...</form>
, not inside<form>
. As the code is at the moment, it will be converted toand we would rather like
After that - have a look at the source code, and see if you can find the csrf field. If you can see it, everything should work in theory.
You can also check that the csrf cookie has been set in your browser, e.g. in Chrome, right-click the web page, and select
Insepect Element
. Select theResources
tab, and click on cookies. You should find a cookie namecsrftoken
there.If you still have problems, double-check the middleware tuple in your
settings.py
and double-check that your browser accept cookier from your server as described above.With Addition of above answer, Try Adding following lines in the views