查找django-registration用户名变量 [英] finding the django-registration username variable
问题描述
好了.
通过django-registration我创建了一个具有密码等的用户.我已将用户命名为Zoro.注册很成功,我花了
With django-registration i have created a user with passwords etc. I have named the user Zoro. The registration was a success and it takes me to
您现在已注册.已发送激活电子邮件."
"You are now registered. Activation email sent."
电子邮件激活已关闭.因此,我进入登录页面并登录.这是在127.0.0.1/accounts/login下完成的.login.html模板成功扩展了base.html模板.在base.html模板中,我有一个标签{{user}},我想在左上方显示用户名.
Email activation is turned off. So i go to login page and login. This is done under 127.0.0.1/accounts/login. The login.html template successfully extends the base.html template. And within the base.html template i have a tag {{ user }}, which i want to show the username at the top left.
当我登录时,我的网址从^/accounts/login扩展到^/homepage.知道我不知道当前用户名的存储位置,因此我无法在视图中将当前用户的名称传递给{{user}}.因此{{用户}}为空.
When i login my urls are extended away from ^/accounts/login to ^/homepage. Know i dont know where the current username is stored, so i cant pass to {{ user }} the curent user's name in views. So {{ user }} is blank.
但是.........如果我返回登录页面,则当前用户名位于左上方.
But.......If i go back to the login page, the current username is at the top left.
我是新来的,请保持温柔.
I am new, be gentle.
推荐答案
您可以使用其他选项将 {{用户}}
添加到模板上下文中:
You have different options to add the {{ user }}
to your templates context:
a)将 django.contrib.auth.context_processors.auth
添加到您的 TEMPLATE_CONTEXT_PROCESSORS
设置中.呈现模板时,这将在所有上下文中提供一个 {{user}}
模板变量.
a) add django.contrib.auth.context_processors.auth
to your TEMPLATE_CONTEXT_PROCESSORS
setting. This will make a {{ user }}
template variable available in all context's when rendering templates.
b)手动执行此操作,并添加一个 user
变量,以将其传递到视图中的上下文.可以从 request.user
b) do it by hand and add a user
variable to pass it to the context in your view. user
can be retrieved from request.user
c)确保没有在某处覆盖 {{user}}
.
c) Make sure you are not overwrting {{ user }}
somewhere.
这篇关于查找django-registration用户名变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!