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

后端 未结 6 1856
挽巷
挽巷 2020-11-28 01:17

I am looking over this website but 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),

6条回答
  •  没有蜡笔的小新
    2020-11-28 01:59

    Update for Django 1.10+:

    is_authenticated is now an attribute in Django 1.10.

    The method was removed in Django 2.0.

    For Django 1.9 and older:

    is_authenticated is a function. You should call it like

    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:

    {% if user.is_authenticated %}
    

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

提交回复
热议问题