Django在主页上的身份验证

huangapple go评论51阅读模式
英文:

Django authentication on home page

问题

我如何在Django首页模板中将登录用户的名称与其他字符串进行比较。

这是我尝试过的

英文:

How can i compare the name of the logged in user on a django home page template with any other string

this is what i tried

答案1

得分: 2

你可以简单地尝试如下:

{% if user.is_superuser %} // 检查用户是否为管理员
    管理员仪表板
{% elif user.is_authenticated %}
    仪表板
{% else %}
    登录
{% end %}

这里的 is_superuser 用于检查用户是否是管理员用户。

英文:

You can simply try like this:

{% if user.is_superuser %} // checks if the user is admin
    Admin Dashboard
{% elif user.is_authenticated %}
    Dashboard
{% else %}
    Sign In
{% end %}

Here is_superuser checks if the user is an admin user or not.

答案2

得分: 0

获取已登录用户的详细信息,您可以这样做:

对于用户名,使用 request.user.username

对于名字,使用 request.user.first_name

其他字段也是类似的。

英文:

to get logged in user details you can do this:

for username request.user.username

for first_name request.user.first_name

same goes for other values ( fields )

huangapple
  • 本文由 发表于 2023年2月24日 17:35:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定