如何在涉及request.user.is_authenticated()和bool对象不可调用的Python Django中修复此错误?

Cha*_*son 3 django python-3.x

我正在尝试为每个用户制作个人资料页面.我添加了一个代码,用于检查用户是否已登录并执行重定向(请参阅下面代码的第12行).

from django.shortcuts import render
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponseRedirect, HttpResponse

from .models import Account, ForSale, WTB

from mysite.forms import MyRegistrationForm

def signup(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/user/')
    else:
        if request.method == 'POST':
            form = MyRegistrationForm(request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect('/user/')

        context = {}
        context.update(csrf(request))
        context['form'] = MyRegistrationForm()

        return render(request, 'signup.html', context)

def index(request):
    return render(request, 'index.html')
Run Code Online (Sandbox Code Playgroud)

但是,在访问/注册/网站时,我收到以下调试消息:

TypeError at /signup/
'bool' object is not callable
Request Method: GET
Request URL:    http://url:8000/signup/
Django Version: 2.0
Exception Type: TypeError
Exception Value:    
'bool' object is not callable
Exception Location: /www/mysite.com/mysite/views.py in signup, line 13
Python Executable:  /usr/bin/python3
Python Version: 3.5.2
Python Path:    
    ['/www/mysite.com',
    '/usr/lib/python35.zip',
    '/usr/lib/python3.5',
    '/usr/lib/python3.5/plat-x86_64-linux-gnu',
    '/usr/lib/python3.5/lib-dynload',
    '/usr/lib/python3.5/site-packages',
    '/usr/local/lib/python3.5/dist-packages',
    '/usr/lib/python3/dist-packages']
Server time:    Sun, 3 Dec 2017 18:07:54 -0800
Run Code Online (Sandbox Code Playgroud)

Ash*_*her 9

在旧版本的Django中,request.user.is_authenticated是一种方法.它现在是一个属性,不再需要括号.如果您将代码更改为:

if request.user.is_authenticated:

它应该按预期工作.

有关详细信息,请参阅此处的文档:https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#django.contrib.auth.models.User.is_authenticated