Django中如何检查用户是否匿名?

发布于 2024-10-11 18:24:47 字数 195 浏览 8 评论 0原文

def index(request):
    the_user = request.user

在Django中,我如何知道它是否是真实用户?我尝试过:

if the_user: 但即使没有人登录,“AnonymousUser”仍然存在。因此,它总是返回 true,这是行不通的。

def index(request):
    the_user = request.user

In Django, how do I know if it's a real user or not? I tried:

if the_user: but "AnonymousUser" is there even if no one logs in. So, it always returns true and this doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

弱骨蛰伏 2024-10-18 18:24:47

您可以检查是否 request.user.is_anonymous 返回 True

You can check if request.user.is_anonymous returns True.

万劫不复 2024-10-18 18:24:47

另一种方法

if user.is_anonymous():
    # user is anon user

是通过测试来查看用户对象的 id 是什么:

if user.id == None:
    # user is anon user
else:
    # user is a real user

请参阅 https ://docs.djangoproject.com/en/dev/ref/contrib/auth/#anonymous-users

An Alternative to

if user.is_anonymous():
    # user is anon user

is by testing to see what the id of the user object is:

if user.id == None:
    # user is anon user
else:
    # user is a real user

see https://docs.djangoproject.com/en/dev/ref/contrib/auth/#anonymous-users

蔚蓝源自深海 2024-10-18 18:24:47

您应该检查 的值request.user.is_authenticated。对于 User 实例,它将返回 True;对于 AnonymousUser 实例,它将返回 False

一个答案建议使用is_anonymous,但是django.contrib.auth 文档 说“你应该更喜欢使用 is_authenticatedis_anonymous”。

You should check the value of request.user.is_authenticated. It will return True for a User instance and False for an AnonymousUser instance.

One answer suggests using is_anonymous but the django.contrib.auth documentation says “you should prefer using is_authenticated to is_anonymous”.

我遇到了类似的问题,只不过这是在 login_redirect_url 发送到的页面上。我必须输入模板:

{% if user.is_authenticated %}
    Welcome Back, {{ username }}
{% endif %}

I had a similar issue except this was on a page that the login_redirect_url was sent to. I had to put in the template:

{% if user.is_authenticated %}
    Welcome Back, {{ username }}
{% endif %}
若有似无的小暗淡 2024-10-18 18:24:47

我知道我在这里做了一些坟墓挖掘,但谷歌搜索将我带到了这个页面。

如果你的视图定义要求用户登录,你可以实现 @login_required 装饰器:

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    …

I know I'm doing a bit of grave digging here, but a Google search brought me to this page.

If your view def requires that the user is logged in, you can implement the @login_required decorator:

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    …
甜中书 2024-10-18 18:24:47

在 Django Rest 框架中,最好使用权限类来检查用户是否经过身份验证。这将适用于整个视图集。最简单的形式可能如下所示:

from rest_framework.permissions import IsAuthenticated
...
class SomeViewSet(viewsets.GenericViewSet):
    permission_classes = [IsAuthenticated]

In Django rest framework it's good idea to use permission classes to check if user is authenticated. This will apply to whole viewSet. In the simpliest form it could look like this:

from rest_framework.permissions import IsAuthenticated
...
class SomeViewSet(viewsets.GenericViewSet):
    permission_classes = [IsAuthenticated]
风为裳 2024-10-18 18:24:47

您应该使用 request.user.is_authenticated 因为它是 request.user.is_anonymous (接受的答案)

You should use request.user.is_authenticated as it is the preferred solution over the request.user.is_anonymous (the accepted answer)

倾城月光淡如水﹏ 2024-10-18 18:24:47

您可以使用 is_authenticated< /a> 和 is_anonymous HttpRequest.user< /a> 和 get_user(request) 到检查 Django 视图中的用户是否是匿名的,如下所示。 *文档 建议使用 is_authenticated 而不是 is_anonymous

# "views.py"

from django.shortcuts import render, 
from django.contrib.auth import get_user

def test(request):
    print(request.user.is_authenticated) # True or False
    print(request.user.is_anonymous) # True or False
    print(get_user(request).is_authenticated) # True or False
    print(get_user(request).is_anonymous) # True or False
    return render(request, 'index.html')

并且,您可以在 Django 模板中检查用户是否是匿名的,如下所示:

{% "index.html" %}

{{ user.is_authenticated }} {% True or False %}
{{ user.is_anonymous }} {% True or False %}

You can use is_authenticated and is_anonymous with HttpRequest.user and get_user(request) to check if the user is anonymous or not in Django Views as shown below. *The doc recommends is_authenticated instead of is_anonymous:

# "views.py"

from django.shortcuts import render, 
from django.contrib.auth import get_user

def test(request):
    print(request.user.is_authenticated) # True or False
    print(request.user.is_anonymous) # True or False
    print(get_user(request).is_authenticated) # True or False
    print(get_user(request).is_anonymous) # True or False
    return render(request, 'index.html')

And, you can check if the user is anonymous or not in Django Templates as shown below:

{% "index.html" %}

{{ user.is_authenticated }} {% True or False %}
{{ user.is_anonymous }} {% True or False %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文