Humberto Morales Humberto Morales - 4 months ago 17
Python Question

django 1.8.13 'User' object has no attribute 'user'

I have a problem with the method Login(), the variable 'user' if it contains the user but that method fails. could help detect the problem.

from django.contrib.auth import authenticate, login

def login(request):
if request.user.is_authenticated():
return render(request, 'Default.html')

mensaje = ''
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
User = authenticate(username=username, password=password)
if User is not None:
if User.is_active:
login(User)
return render(request, 'Default.html')
else:
return render(request, 'accounts/login.html', {'mensaje':mensaje})
else:
return render(request, 'accounts/login.html', {'mensaje':mensaje})

Answer

You are importing login from django.contrib.auth but look at your very next line of code.

from django.contrib.auth import authenticate, login

def login(request):
    if request.user.is_authenticated():
        return render(request, 'Default.html')

Yep, you are shadowing that login method with one for your own. The solution would be to rename your own funciton

def my_login(request):
    if request.user.is_authenticated():
        return render(request, 'Default.html')