CSRF verification failed. Request aborted.

103 views
Skip to first unread message

plogu...@yahoo.com.cn

unread,
May 18, 2012, 10:20:39 PM5/18/12
to Django users
I find a code, but the Django version is 0.9, I am using 1.4 and I am
a new learner, I want to run below code but report CSRF verification
failed.

from django.http import HttpResponse

text = """<form method="post" action="/add/">
<input type="text" name="a" value="%d"> + <input type="text"
name="b" value="%d">
<input type="submit" value="="> <input type="text" value="%d">
</form>"""

def index(request):
if request.POST.has_key('a'):
a = int(request.POST['a'])
b = int(request.POST['b'])
else:
a = 0
b = 0
return HttpResponse(text % (a, b, a + b))

Kurtis Mullins

unread,
May 18, 2012, 10:54:46 PM5/18/12
to django...@googlegroups.com
In your template, you need to include {% csrf_token %} within your <form></form> block. Also, make sure HttpResponse works correctly with CSRF. It may, or it may not -- but I think I remember reading that you need to use the 'render' method to make that template tag work.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
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.


银涛 徐

unread,
May 18, 2012, 11:02:48 PM5/18/12
to django...@googlegroups.com
It's difficult for me.
I am just learning it, so hard.

--- 12年5月19日,周六, Kurtis Mullins <kurtis....@gmail.com> 写道:

发件人: Kurtis Mullins <kurtis....@gmail.com>
主题: Re: CSRF verification failed. Request aborted.
收件人: django...@googlegroups.com
日期: 2012年5月19日,周六,上午10:54

doniyor

unread,
May 19, 2012, 1:57:05 AM5/19/12
to django...@googlegroups.com
as Kurtis said, you need  {% csrf_token %}  and your rendering response should be something like this: 
return render_to_response('index.html', {'your_key': your_value},context_instance=RequestContext(request))   and those your value will be rendered thru youe_key tag. 

here is how your form should look like: 
text = """<form method="post" action="/add/"> {% csrf_token %} 
Reply all
Reply to author
Forward
0 new messages