简体   繁体   中英

how to determine the status of the user?

please help to solve the problem.

is a function that defines the user logged in or not: views.py:

def ajax_is_authenticated_check(request):
    """
    ajax check auth for like process and any
    """ 
    result = False

    if request.method == 'POST' and request.is_ajax():
        if request.user.is_authenticated:
            result = True

    data = {
        'is_authenticated': result
    }

    print(result)   #this is output to console

    return HttpResponse(json.dumps(data), content_type='application/json')  

resulting function always returns "true". and output console "true".

it is not clear why, if the user has not entered, it still comes back the "true"

That's because is_authenticated is a method, not a property.

Code should look like this

if request.method == 'POST' and request.is_ajax():
        if request.user.is_authenticated(): # note the ()
            result = True

From Django 1.10 onwards is_authenticated is a property and you can use request.user.is_authenticated .

For version below 1.10 you need to use request.user.is_authenticated() in views.

Note: In templates for all versions you can use request.user.is_authenticated .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM