2

私のbase.htmlファイルでは、 ここを使用しています。ユーザーがログインしていても、ログインボタンが表示されます。
{% if user.is_authenticated %}
<a href="#">{{user.username}}</a>
{% else %} <a href="/acc/login/">log in</a>

リンクをクリックするlog inと、ユーザー名と通常のログインビューが表示され、ユーザーがログインしていることが示されます。

それで、何が問題なのですか?

4

2 に答える 2

5

テンプレートにユーザー情報が含まれていないようです。あなた'django.contrib.auth.middleware.AuthenticationMiddleware'はあなたのMIDDLEWARE_CLASSES設定で必要であり、あなたのテンプレートの文脈でその良さを得るために、あなたはする必要があります:

from django.shortcuts import render_to_response
from django.template import RequestContext

def my_view(request):
    return render_to_response('my_template.html',
                              my_data_dictionary,
                              context_instance=RequestContext(request))

どこでもこれを行う手間を省くために、の代わりにdjango-annoyingの render_toデコレータを使用することを検討してくださいrender_to_response

@render_to('template.html')
def foo(request):
    bar = Bar.object.all()
    return {'bar': bar}

# equals to
def foo(request):
    bar = Bar.object.all()
    return render_to_response('template.html',
                              {'bar': bar},
                              context_instance=RequestContext(request))
于 2010-05-16T06:45:17.997 に答える
1

ドミニク・ロジャーの答えがあなたの問題を解決すると確信しています。direct_to_template代わりにインポートすることを個人的に好むことを追加したかっただけですrender_to_response

from django.views.generic.simple import direct_to_template
...
return direct_to_template(request, 'my_template.html', my_data_dictionary)

しかし、それは好みの問題だと思います。私の場合、代わりに名前付きパラメータを使用することもできますmy_data_dictionary:

return direct_to_template(request, 'template.html', foo=qux, bar=quux, ...)
于 2010-05-16T13:42:40.107 に答える