Django用户名根本不显示

时间:2013-11-16 21:49:26

标签: python django django-templates django-authentication

我是Django的绝对初学者。我已经按照本教程: http://www.rdegges.com/user-authentication-with-django/

经过一些改变,设法让它在新版本上运行。 只有一个问题。登录后,它说:

<h2>Welcome, {{ user.username }}</h2>

应显示当前登录用户的用户名。 即使一切正常,我正在登录并转移到这个网站,它只是说:&#34;你好,&#34;并且不会在{{ user.username }}的位置生成任何内容。任何人都知道我可能会缺少什么?

2 个答案:

答案 0 :(得分:3)

我解决了您的问题,问题是render_to_responsedjango.template.RequestContext文件uploader/main/views.py应该是这样的:

from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
rom django.template import RequestContext

@login_required
def main_main_page(request):
    return render_to_response('main/index.html', context_instance=RequestContext(request))

答案 1 :(得分:2)

编辑:只要项目设置中的TEMPLATE_CONTEXT_PROCESSORS中有“django.core.context_processors.auth”,就不需要添加请求来引用模板中经过身份验证的用户。

您的核心问题是通过@juliocesar在其答案中描述的context_instance将请求传递给模板。

原始回答:

尝试

<h2>Welcome, {{ request.user.username }}</h2>

教程中的视图似乎没有定义并将'user'变量返回给模板,但您可以在请求中访问经过身份验证的用户信息。

相关问题