普通に使っている人にとっては常識すぎる内容だけど。
コード内に日本語書いたらエラーが出た
コードの1行目に以下を追加。
# -*- coding: utf-8 -*-
POSTしたらForbiddenになった
CSRF verification failed. Request aborted.
settings.py に追記(Django1.4.1だと設定は不要だった)
settings.py
MIDDLEWARE_CLASSES = (
…
'django.middleware.csrf.CsrfViewMiddleware',
…
テンプレートファイルのフォームの中に以下を追加
{% csrf_token %}
ビューファイルの変更(チュートリアルで作成したコードの場合)
from django.template import RequestContext
…
def detail(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
#return render_to_response('polls/detail.html', {'poll': p})
return render_to_response('polls/detail.html', {'poll': p}, context_instance=RequestContext(request))
…
参考
https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ref-contrib-csrf