| « Djangoのモデルでuser値を・・・ | Ubuntu AMD64 SunBird日本語化 » |
Djangoのrender_to_response()ショートカットでハマった
ことだまんです。
タイトル通りハマりました。
ショートカットを使用し辞書オブジェクトを渡す場合はContextだけの扱いになってRequestContextは渡されないのね。
TEMPLATE_CONTEXT_PROCESSORSでdjango.core.context_processors.authを設定しているのにuser情報がdumpできなくてハマりました。
Django の render_to_response() ショートカットを使っていて、辞書オブジェクトを渡してコンテキストの変数を追加している場合、テンプレートはデフォルトで (RequestContext ではなく) Context になります。テンプレートのレンダリングに RequestContext を使うには、 render_to_response() の 3 つめの引数に RequestContext インスタンスを指定します。コードは以下のようになるでしょう:
Code:
def some_view(request):
# ...
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
Djangoマニュアル和訳:
http://michilu.com/django/doc-ja/templates_python/#django-core-context-processors-auth