尝试获取用户名时的问题

时间:2021-04-08 14:02:40

标签: python django

当我运行这个时:

from django.shortcuts import render  
from django.http import HttpResponse  
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login as auth_login
  
def login(request):
    if request.user.is_authenticated():
        username = request.user.username
    else:
        username = None
    return render (request, 'login_success.html', {'username': username})

我收到此错误:

TypeError at /login/success/
'bool' object is not callable

Here's a pic:

这是我的 html:

{% include 'base.html' %}

<text align="center">Sup {{ username }}, you're logged in!</text>

该错误是什么意思,我该如何解决?

1 个答案:

答案 0 :(得分:2)

is_authenticated 是一个属性,你只需要去掉括号

https://docs.djangoproject.com/en/3.1/ref/contrib/auth/#attributes