CSRF verification failed. Request aborted With Django1.5
(error内容)
CSRF verification failed. Request aborted.
原因はそのエラー画面の続きに書いてあった。
Your browser is accepting cookies.
・The view function uses RequestContext for the template, instead of Context.
・In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
・If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
上記3つを満たしていればいいらしい。
(1). Contextの代わりにRequestContextを使え。
render_to_response('news/index.html',
{'entry': entry},
context_instance=RequestContext(request))
OKちゃんと使った。
(2). POSTするときには{% csrf_token %}をつけろ。
OKちゃんとつけた。
(3). Viewにcsrf_protectを使え。
from django.views.decorators.csrf import csrf_protect
@csrf_protect
def search(request):
............
OKちゃんと使った。
再実行すると問題なくPOSTできた。
参考:
Cross Site Request Forgery protection
- 関連記事
-
- mecab-python with Ubuntu12.04 LTS
- CSRF verification failed. Request aborted With Django1.5
- Django1.5 DEBUG=Falseで500 Error