如何修复我的 Django 网站中的 "bool object is not callabe" 错误

how do I fix the "bool object is not callabe" error in my Django website

所以我正在尝试修复一个不属于我的网站,出于某种原因,当我单击按钮进行注册时出现此错误:

 File "C:\Users\eneko\Desktop\viveWeb\blog\views.py", line 20, in login_view
 if request.user.is_authenticated():
 TypeError: 'bool' object is not callable

这是验证用户注册的视图:

def login_view(request):
if request.user.is_authenticated():
    return redirect(reverse('portada'))
mensaje = 'El usuario no estaba registrado'
if request.method == 'POST':
    username = request.POST.get('username')
    dia = request.POST.get('dia')
    mes = request.POST.get('mes')
    year = request.POST.get('year')
    password =  dia + mes + year + 'year'
    user = authenticate( username=username, password=password)
    if user is not None:            
        if user.is_active:
            login(request, user)
            return redirect(reverse('portada'))
        else:
            return render(request, 'accounts/login.html', {'error_message': 'Your account has been disabled'})    
    else:
        mensaje = 'Se va a registrar un usuario'
        form = RegistroUserForm(request.POST, request.FILES)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            dia = form.cleaned_data['dia']
            mes = form.cleaned_data['mes']
            year = form.cleaned_data['year']

            password =  dia + mes + year + 'year'
            user_model = User.objects.create_user(username=username, password=password)
            user_model.save()
            
            user_profile = UserProfile()
            user_profile.user = user_model
            user_profile.save()
           
            username = request.POST.get('username')
            dia = request.POST.get('dia')
            mes = request.POST.get('mes')
            year = request.POST.get('year')
            password =  dia + mes + year + 'year'
            user = authenticate(username=username, password=password)
            if user is not None:
                mensaje = 'Se va a registrar un usuario12'
                if user.is_active:
                    login(request, user)
                    mensaje = 'Se va a registrar un usuario123'
                    return redirect(reverse('portada'))
                
            return render(request, 'blog/crearVivencia.html', {'mensaje': mensaje})
        pass    
return render(request, 'accounts/login.html', {'mensaje': mensaje})

出于某种原因,我无法访问显示注册表单的页面。正如我所说,这段代码不是我的,但我会澄清任何疑问。

非常感谢您的帮助。

错误很明显:

 TypeError: 'bool' object is not callable

你正在使用一个bool变量作为函数,根据docs is_authenticated是一个属性。 要修复它,只需写:

if request.user.is_authenticated:

TypeError: 'bool' object is not callable 当您尝试将对象当作方法或函数时显示。

在你的第二行

if request.user.is_authenticated():

改为

if request.user.is_authenticated:

因为它不可调用