[Django]-AttributeError at /accounts/login/ 'User' object has no attribute 'user'

5👍

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 from django.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 view login, 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, taking request from the first parameter which usually is the request – but in your case, you’ve passed user as 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 auth and call auth.login(user) inside your view.

1👍

login must have 2 arguments, request and user. If the request isn’t given login can’t set cookies etc. So do:

login(request, user)

https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.login

Leave a comment