Monday, July 23, 2012

Re: CSRF token missing or incorrect

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.

On Mon, Jul 23, 2012 at 12:14 PM, twelve_o_clock <twelve_o_clock@lavabit.com> wrote:

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')

--
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-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@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

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

No comments:

Post a Comment