CSRF token missing or incorrect

85 views
Skip to first unread message

twelve_o_clock

unread,
Jul 23, 2012, 2:14:00 PM7/23/12
to django...@googlegroups.com

When I try to submit a form in my django project, I get an error saying, "CSRF token missing or incorrect," even though I put a "{% csrf_token %}" tag inside the form.

Here is signup.html:

<html>
<head>
<title>
Sign up
</title>
</head>
<body>
<form method="POST" action="/signup/">
 {% csrf_token %}
 Name: <input type="text" /><br />
 E-mail address: <input type="email" /><br />
 Password: <input type="password" /><br />
 <input type="submit" value="Submit" />
</form>
</body>
</html>

Here is a part of views.py:

def signup(request):
    if request.method == 'POST':
        user = User.new()
        user.name = request.POST["name"]
        user.email = request.POST["email"]
        user.password = request.POST["password"]
        return HttpResponse("User created")
    else:
        return render_to_response('micropost/signup.html')

Jonathan Baker

unread,
Jul 23, 2012, 2:18:55 PM7/23/12
to django...@googlegroups.com
You need to add a RequestContext to the render_to_response function:
return render_to_response('micropost/signup.html', context_instance=RequestContext(request))


I'd also recommend using a form class so that you can utilize methods like is_valid() and the cleaned_data dict once the form is bound.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/3dj5GbXKavAJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Jonathan D. Baker
Developer
http://jonathandbaker.com

Reply all
Reply to author
Forward
0 new messages