Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix this error in Python Django involving request.user.is_authenticated() and bool object not callable?

I am trying to make profile pages for each user. I added a code that checks if the user is logged in and does a redirect (see line 12 of the code below).

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

However, upon accessing /signup/ on the site I get the following debug message:

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
like image 665
Charles T. Avatar asked Jan 17 '26 05:01

Charles T.


1 Answers

In older versions of Django request.user.is_authenticated was a method. It's now an attribute and no longer requires parenthesis. If you change your code to:

if request.user.is_authenticated:

It should be work as expected.

For more info see the docs here: https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#django.contrib.auth.models.User.is_authenticated

like image 154
Asher Avatar answered Jan 19 '26 20:01

Asher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!