gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:01:30 24 4
gpt4 key购买 nike

我正在尝试为每个用户制作个人资料页面。我添加了一个代码来检查用户是否已登录并进行重定向(参见下面代码的第 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')

但是,在访问站点上的/signup/时,我收到以下调试消息:

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

最佳答案

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

如果 request.user.is_authenticated:

它应该按预期工作。

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

关于django - 如何在涉及 request.user.is_authenticated() 和 bool 对象不可调用的 Python Django 中修复此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47625640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com