0

I don't know how to get around this CSRF failure. I've included the {% csrf_token %} that I keep reading about but it still gives me this error. Here is the code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Log in</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <style>
        body{
        font-family:Arial,Helvetica,sans-serif;
            font-size: 12px;
        }
    </style>
    </head>
    <body>
        {{ state }}
        <form action="/login/" method="post">{% csrf_token %}
            {% if next %}
            <input type="hidden" name="next" value="{{ next }}" />
            {% endif %}
            username:
            <input type="text" name="username" value="{{ username}}" /><br />
            password:
            <input type="password" name="password" value="" /><br />

            <input type="submit" value="Log In" />
        </form>
    </body>
</html>

I've included all the proper features for this but if you need to see any more code let me know...I think this is all that applies though...

EDIT: views.py

from django.shortcuts import render_to_response
from django.contrib.auth import *

def login_user(request):
  state = "Please log in below..."
  username = password = ''
  if request.POST:
      username = request.POST.get('username')
      password = request.POST.get('password')

      user = authenticate(username=username, password=password)
      if user is not None:
          if user.is_active:
              login(request, user)
              state = "You're successfully logged in!"
          else:
              state = "Your account is not active, please contact the site admin."
      else:
          state = "Your username and/or password were incorrect."

  return render_to_response('auth.html',{'state':state, 'username': username})
5
  • What is "this error"? Can you post a full traceback of the error you're seeing? Can you post the view code? It'll help us better diagnose your issue.
    – Sam Dolan
    Mar 8, 2013 at 22:45
  • Are you including just the token without HTML markup? Mar 8, 2013 at 22:46
  • ^^^ It should be included that way. OP: have you inspected the HTML to see if the {% csrf_token %} tag actually generates the hidden input element? Otherwise review the points 1. to 3. here: docs.djangoproject.com/en/dev/ref/contrib/csrf
    – Tobia
    Mar 8, 2013 at 22:47
  • The error is just what's stated, it is a CSRF verification failure. From what i can see, there is no traceback log. I've added the view.py file if it help diagnose the issue. Mar 9, 2013 at 0:05
  • please post your settings codes
    – catherine
    Mar 9, 2013 at 0:31

2 Answers 2

3

You need to add the token to the template context, try something like this, and read up on the documentation/tutorial linked to in the comments to your question.

from django.core.context_processors import csrf

def login_user(request):
    ...your code...
    ctx = {'state':state, 'username': username}
    ctx.update(csrf(request))
    return render_to_response('auth.html',ctx)
1
  • 3
    Or just use render instead of render_to_response.
    – wRAR
    Mar 9, 2013 at 1:18
0

In your view,return a context_instance to the template as follows

from django.template import RequestContext
def login_user(request):
    ...
    return render_to_response('auth.html',{'state':state, 'username':
     username},context_instance=RequestContext(request))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.