Vai
Vai

Reputation: 353

How to solve a attribute error in Django?

I am trying to log in a user but I am getting an attribute error.

Here is my forms.py:

class Login(forms.Form):
    email = forms.EmailField(max_length=250)
    password = forms.CharField(widget=forms.PasswordInput)

    def login_user(self):
        email = self.cleaned_data['email']
        password = self.cleaned_data.get('password')
        user = authenticate(email=email, password=password)
        if user in User.objects.all():
            login(self, user)
        else:
            return render(self, 'todoapp/waiting_2.html')

Here is my views.py:

def login_user(request):
    if request.method == 'POST':
        login_form = Login(request.POST)
        if login_form.is_valid():
            login_form.login_user()
            login_form.save()
            return HttpResponseRedirect(reverse('dashboard'))
        else:
            return render(request, 'todoapp/waiting_2.html')
    return render(request, 'registration/login.html', {'form': Login()})

When I fill in the fields and try to log in, I am getting the error:

AttributeError at /login/

'Login' object has no attribute 'session'

Traceback:

File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/gblp250/PycharmProjects/assignment/todoapp/views.py" in login_user
  48.             login_form.login_user(request)

File "/home/gblp250/PycharmProjects/assignment/todoapp/forms.py" in login_user
  27.             login(self, request, user)

File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in login
  126.     if SESSION_KEY in request.session:

Exception Type: AttributeError at /login/
Exception Value: 'Login' object has no attribute 'session'

Upvotes: 0

Views: 2203

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599450

There are a few errors here. The main one of attempting to render within the form login_user method. Apart from anything else, you attempt to pass the self as the request parameter to render, which mages no sense.

Remove all of that if/else. You don't need to render; but also note that your if condition is needlessly inefficient. If you get a user, it's necessarily a User.

Finally, the actual cause of your error, where again you are trying to pass self in the place of a request, but this time as the parameter to login. That code belongs in the view.

And finally, the form is not a ModelForm, so there is no save method.

So, form:

def login_user(self):
    email = self.cleaned_data['email']
    password = self.cleaned_data.get('password')
    return authenticate(email=email, password=password)

and view:

    if login_form.is_valid():
        user = login_form.login_user()
        if user:
            login(request, user)
        return HttpResponseRedirect(reverse('dashboard'))

Although at this point you may as well move all that logic to the view.

Upvotes: 3

eran
eran

Reputation: 6921

login() get as first argument the request, you call it with the form as first argument. https://docs.djangoproject.com/en/2.1/topics/auth/default/#django.contrib.auth.login

Upvotes: 0

Related Questions