我有一个视图,并在用户中登录:
def login_user(request):
c = {}
c.update(csrf(request))
username = password = ''
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
c.update(user(request))
return redirect('/module/')
else:
return redirect('/inactive/')
else:
return redirect('/failure/')
return render_to_response('core/auth.html',c)
这会正确登录用户,因为如果我是超级用户,我可以访问django管理页面。
登录并重定向后,我想在屏幕上显示用户名,目前我正在使用
{% if user.is_authenticated %}
<p class="navbar-text pull-right">Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
<p class="navbar-text pull-right">Welcome, new user. Please log in.</p>
{% endif %}
但它似乎总是相信用户没有登录。任何帮助都会受到赞赏。
编辑:这是我的模板上下文处理器
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
)
答案 0 :(得分:6)
您是否尝试过向模板发送RequestContext对象?
return render_to_response('core/auth.html', c, context_instance=RequestContext(request))
答案 1 :(得分:1)