Python 如何检查用户是否登录(如何正确使用 user.is_authenticated)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3644902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:03:58  来源:igfitidea点击:

How to check if a user is logged in (how to properly use user.is_authenticated)?

pythondjangoauthentication

提问by Rick

I am looking over this websitebut just can't seem to figure out how to do this as it's not working. I need to check if the current site user is logged in (authenticated), and am trying:

我正在查看此网站,但似乎无法弄清楚如何执行此操作,因为它不起作用。我需要检查当前站点用户是否已登录(经过身份验证),并且正在尝试:

request.user.is_authenticated

despite being sure that the user is logged in, it returns just:

尽管确定用户已登录,但它只返回:

>

I'm able to do other requests (from the first section in the url above), such as:

我可以执行其他请求(来自上面 url 的第一部分),例如:

request.user.is_active

which returns a successful response.

它返回一个成功的响应。

采纳答案by Brian Neal

Update for Django 1.10+: is_authenticatedis now an attribute in Django 1.10. The method still exists for backwards compatibility, but will be removed in Django 2.0.

Django 1.10+ 的更新is_authenticated现在是 Django 1.10 中的一个属性。为了向后兼容,该方法仍然存在,但将在 Django 2.0 中删除。

For Django 1.9 and older:

对于 Django 1.9 及更早版本

is_authenticatedis a function. You should call it like

is_authenticated是一个函数。你应该这样称呼它

if request.user.is_authenticated():
    # do something if the user is authenticated

As Peter Rowell pointed out, what may be tripping you up is that in the default Django template language, you don't tack on parenthesis to call functions. So you may have seen something like this in template code:

正如 Peter Rowell 指出的那样,可能会让您感到困惑的是,在默认的 Django 模板语言中,您没有附加括号来调用函数。所以你可能在模板代码中看到过这样的事情:

{% if user.is_authenticated %}

However, in Python code, it is indeed a method in the Userclass.

但是,在Python代码中,它确实是User类中的一个方法。

回答by Sopan

Following block should work:

以下块应该工作:

    {% if user.is_authenticated %}
        <p>Welcome {{ user.username }} !!!</p>       
    {% endif %}

回答by Mark Chackerian

Django 1.10+

Django 1.10+

Use an attribute, nota method:

使用属性,而不是方法:

if request.user.is_authenticated: # <-  no parentheses any more!
    # do something if the user is authenticated

The use of the method of the same name is deprecated in Django 2.0, and is no longer mentioned in the Django documentation.

使用同名方法在 Django 2.0 中已弃用,在 Django 文档中不再提及。



请注意,对于 Django 1.10 和 1.11,属性的值是 aCallableBoolCallableBool而不是布尔值,这会导致一些奇怪的错误。例如,我有一个返回 JSON 的视图

return HttpResponse(json.dumps({
    "is_authenticated": request.user.is_authenticated()
}), content_type='application/json') 

that after updated to the property request.user.is_authenticatedwas throwing the exception TypeError: Object of type 'CallableBool' is not JSON serializable. The solution was to use JsonResponse, which could handle the CallableBool object properly when serializing:

更新到属性request.user.is_authenticated后抛出异常TypeError: Object of type 'CallableBool' is not JSON serializable。解决方案是使用 JsonResponse,它可以在序列化时正确处理 CallableBool 对象:

return JsonResponse({
    "is_authenticated": request.user.is_authenticated
})

回答by Cubiczx

In your view:

在您看来:

{% if user.is_authenticated %}
<p>{{ user }}</p>
{% endif %}

In you controller functions add decorator:

在您的控制器函数中添加装饰器:

from django.contrib.auth.decorators import login_required
@login_required
def privateFunction(request):

回答by Jatin Goyal

For Django 2.0+versions use:

对于Django 2.0+版本,请使用:

    if request.auth:
       # Only for authenticated users.

For more info visit https://www.django-rest-framework.org/api-guide/requests/#auth

有关更多信息,请访问https://www.django-rest-framework.org/api-guide/requests/#auth

request.user.is_authenticated() has been removed in Django 2.0+ versions.

request.user.is_authenticated() 已在 Django 2.0+ 版本中删除。

回答by Suyash Kumar

If you want to check for authenticated users in your template then:

如果要在模板中检查经过身份验证的用户,请执行以下操作:

{% if user.is_authenticated %}
    <p>Authenticated user</p>
{% else %}
    <!-- Do something which you want to do with unauthenticated user -->
{% endif %}