LoginSignup
10
11

More than 5 years have passed since last update.

はじめてのDjangoで躓いた箇所まとめ

Last updated at Posted at 2012-09-01

普通に使っている人にとっては常識すぎる内容だけど。

コード内に日本語書いたらエラーが出た

コードの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

10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11