在base.html (django) 中显示登录用户

anc*_*evv 2 django django-templates

有没有办法访问 base.html 文件中登录用户的名字?

我正在尝试这样做,因为我想在导航栏上显示当前登录的用户,但它不会访问用户的信息,也不会正确检查用户是否经过身份验证。

base.html 内的 html

            Hi there,

            {% if user.is_authenticated %}

                {{user.first_name}}
            {% else %}
                Stranger
            {% endif %}
Run Code Online (Sandbox Code Playgroud)

sup*_*er9 5

request.user为您提供当前登录的用户对象。因此您可以完全访问 User 类具有的所有属性和方法。为了得到first_name,你可以做{{ request.user.first_name }}。获取您使用的全名{{ request.user.get_full_name }}