fc2ブログ

2013-03-24(Sun)

CSRF verification failed. Request aborted With Django1.5

Django1.5を使ってformからPOSTしようとしたら以下のエラーが発生した。

(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 %}をつけろ。


{% 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


関連記事
スポンサーサイト



コメントの投稿

管理者にだけ表示を許可する

コメント

プロフィール

kumagonjp2

Author:kumagonjp2
Python,Django,R,Mongo,MySQL,Struts,Spring,データマイニングなどサーバー関係のメモを残していきます。

最新記事
最新コメント
最新トラックバック
月別アーカイブ
カテゴリ
雪が3Dで降るブログパーツ ver2

マウスで見る方向変えられます

検索フォーム
RSSリンクの表示
リンク
ブロとも申請フォーム

この人とブロともになる

QRコード
QR