[英]How I can print informations about user (who is logged) in django template?
I have template like this : 我有这样的模板:
<html>
<body>
You are logged as John Kowalski
</body>
</html>
I would like show first_name and last_name of user but this doesn't work: 我想显示用户的first_name和last_name但这不起作用:
<html>
<body>
You are logged as {{ request.user.first_name }} {{ request.user.last_name }}
</body>
</html>
How can I get user object in template? 如何在模板中获取用户对象?
You need to do a few things: 你需要做一些事情:
First, add the following to your settings: 首先,将以下内容添加到您的设置中:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
)
(You can add other context processors, but this is the relevant one.) (您可以添加其他上下文处理器,但这是相关的。)
Second, in your views: 第二,在你的观点中:
Import this: 导入此:
from django.template import RequestContext
And in your views, return your templates like this: 在您的视图中,返回您的模板,如下所示:
return render_to_response('something.html', context_instance=RequestContext(request))
You can now access the request
object in your template. 您现在可以访问模板中的
request
对象。
Learn more here: https://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext 在此处了解更多信息: https : //docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.