Riv*_*Tam 38 django django-authentication django-registration
从django模板获取用户信息的最佳方法是什么?
例如,如果我只想:
我正在使用django-registration/authentication
use*_*ser 68
当前Django版本的另一种方法:
{% if user.is_authenticated %}
<p>Welcome, {{ user.get_username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
注意:
request.user.get_username()中的意见和user.get_username模板.首选username直接引用属性.资源django.contrib.auth.context_processors.auth 默认情况下启用并包含变量user django.core.context_processors.request模板上下文处理器.资料来源:https://docs.djangoproject.com/en/dev/topics/auth/default/#authentication-data-in-templates
Tim*_*ony 37
{% if request.user.is_authenticated %}Welcome '{{ request.user.username }}'
{% else %}<a href="{% url django.contrib.auth.login %}">Login</a>{% endif %}
Run Code Online (Sandbox Code Playgroud)
并确保request在您的settings.py:中安装了模板上下文处理器:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.request',
...
)
Run Code Online (Sandbox Code Playgroud)
根据问题标题,以下内容可能对某人有用.在我的模板中使用了以下内容:
用户名:{{ user.username }}
用户全名:{{ user.get_full_name }}
用户组:{{ user.groups.all.0 }}
邮箱:{{ user.email }}
会议开始于:{{ user.last_login }}
谢谢 :)
| 归档时间: |
|
| 查看次数: |
52201 次 |
| 最近记录: |