Search code examples
pythondjangodjango-allauth

Django allauth module throw error at login?


This is happen at login submit form, the problem is that I do not know how to debug it

AttributeError at /users/login/ 'NoneType' object has no attribute 'is_active'

Exception Location: C:\projects\Django-project\lib\site-packages\allauth\account\utils.py in perform_login, line 132

project/users/forms.py:

from django import forms
from allauth.account.forms import LoginForm, SignupForm
from django.utils.translation import gettext as _

class LoginForm(LoginForm):
    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.fields['login'].widget = forms.TextInput(attrs={'type': 'text', 'class': 'input-text'})
        self.fields['login'].label = _('Username')
        self.fields['password'].widget = forms.PasswordInput(attrs={'class': 'input-text'})
        self.fields['remember'].label = _('Remember me')

    def clean(self):

        error_dict = {}

        login_data = self.cleaned_data.get('password')
        if len(login_data) < 3:
            error_dict['login'] = forms.ValidationError('Добави още символи2!')


        password_data = self.cleaned_data.get('login')
        if len(password_data) < 3:
            error_dict['password'] = forms.ValidationError('Добави още символи2!')

        if error_dict:
            raise forms.ValidationError(error_dict)
        else:
            return True



class SignupForm(SignupForm):
    def __init__(self, *args, **kwargs):
        super(SignupForm, self).__init__(*args, **kwargs)
        self.fields['username'].widget = forms.TextInput()

    def clean(self):
        error_dict = {}
        username_data = self.cleaned_data.get('username')

        if len(username_data) < 3:
            error_dict['username'] = forms.ValidationError('тест по-малко от 2')

        if error_dict:
            raise forms.ValidationError(error_dict)
        else:
            return True

project/users/views.py:

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.views import generic
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

# Create your views here.

    def redirect_to_user_profile(request):
        url = f'/users/profile/{request.user.id}'
        return HttpResponseRedirect(redirect_to=url)


    class UserProfileView(generic.DetailView):
        model = User
        template_name = 'user_profile.html'

I can provide much more information (code) if need it.

P.S: The problem comes from project/users/forms.py: if I delete whole def clean(self): it works fine.


Solution

  • When overriding a form's clean() method, you need to return the cleaned data. Here the documentation of how it works with an example:

    • Always first call cleaned_data = super().clean() to get an instance of cleaned_data.
    • Always finish by return cleaned_data, because that's what gets assigned to Form.cleaned_data. So don't return True!

    Note that since you're cleaning individual fields, you're better off overriding the individual field cleaning methods clean_<field>, e.g.:

    def clean_login(self):
        login_data = self.cleaned_data['login']
        if len(login_data) < 3:
            raise forms.ValidationError('Добави още символи2!')
        return login_data