I’m new to django and trying to get user authentication working. I have setup a very basic login form and view but I’m getting the error:
AttributeError at /accounts/login/ 'User' object has no attribute 'user'
I’m confused because I don’t try and access User.user
I know it has to be something in the first else statement because an authenticated user just redirects to “/” as it should
Here is the view:
def login(request):
if request.user.is_authenticated():
return HttpResponseRedirect("/")
else:
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(user)
return HttpResponseRedirect("/")
return HttpResponse("There was an error logging you in")
else:
return render_to_response("registration/login.html",
context_instance=RequestContext(request))
The error is raised in views.py, line 15: if request.user.is_authenticated():
Your view function is called
login, and it takes a single parameter,request.In line 11 of your view, you call
login(user). Now, you probably meant that to be the login function fromdjango.contrib.auth, and presumably you have imported it from there at the top of the view. But Python can only have one thing using a name at once: so when you named your viewlogin, it overwrote the existing reference to that name.The upshot of this is that that line calls your view, not the login function. (That’s why you’re getting that particular error: the first line of your view checks
request.user, takingrequestfrom the first parameter which usually is the request – but in your case, you’ve passeduseras the first parameter, and of course user doesn’t itself have a user param.)The solution is to either rename your view to something else, or do
from django.contrib import authand callauth.login(user)inside your view.