在django模板中获取用户信息 [英] Get user information in django templates

查看:238
本文介绍了在django模板中获取用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从django模板获取用户信息的最佳方式是什么?

What's the best way to get user information from a django template?

例如,如果我只想:


  1. 如果用户登录,请显示Welcome [username]

  2. 否则,显示登录按钮。

我正在使用django-registration / authentication

I'm using django-registration/authentication

推荐答案

{% if request.user.is_authenticated %}Welcome '{{ request.user.username }}'
{% else %}<a href="{% url django.contrib.auth.login %}">Login</a>{% endif %}

,并确保您的 settings.py 中安装了请求模板上下文处理器:

and make sure you have the request template context processor installed in your settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'django.core.context_processors.request',
    ...
)

这篇关于在django模板中获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆