Search code examples
djangoformsdjango-formsregistration

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


So I am trying to fix a website that is not mine, for some reason when I click the button to register I get this error:

 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

This is the view that authenticates the user to register:

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})

For some reason I can't access the page that shows the registration form. As I've said this code is not mine but I will clarify any doubt.

Help is much appreciated.


Solution

  • TypeError: 'bool' object is not callable is shown when you are trying to behave an object like it is a method or function.

    In your second line

    if request.user.is_authenticated():
    

    Change it to

    if request.user.is_authenticated:
    

    Because it's not callable