LoginView correct login details are incorrect

Hello,

I’m able to register new users in ‘My User’ db but when i try to login it fails with wrong details, authentication with superUser on Django db works.

MyUser in models.py:

class MyUser(AbstractBaseUser):
    # first name specs
    first_name = models.CharField(
        verbose_name='first_name', max_length=255, blank=False,
        error_messages={
            'blank': 'First name required!',
            'null': 'First name required!'
        }
    )
    # last name specs
    last_name = models.CharField(
        verbose_name='last_name', max_length=255, blank=False,
        error_messages={
            'blank': 'Last name required!',
            'null': 'Last name required!'
        }
    )

    # email specs
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True, validators=[validators.EmailValidator], error_messages={
            'unique': 'The email address may already exist on the system!',
            'blank': 'email address is required!',
            'invalid': 'Invalid email address!',
            'null': 'email address is required!'
        }
    )

    # username specs
    username = models.CharField(
        verbose_name='username', max_length=30, unique=True, blank=False, validators=[validators.RegexValidator('^[a-z0-9_-]{3,15}$')], error_messages={
            'unique': 'This username may exist on the system!',
            'blank': 'Username is required!',
            'invalid': 'Invalid username!',
            'null': 'Username required!'
        }
    )
    is_active = models.BooleanField(default=True)
    EMAIL_FIELD = 'email'
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['first_name', 'last_name', 'email']

urls.py:

path('login', LoginView.as_view(
        template_name='login.html'), name="login"),

settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tags.apps.TagsConfig',
    'notes.apps.NotesConfig',
    'pages.apps.PagesConfig',
]

apps.py:

from django.apps import AppConfig


class PagesConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    AUTH_USER_MODEL = 'pages.MyUser'
    name = 'pages'

How are you registering new users? Are you doing it with your own view and form? Or are you registering them through the Django Admin?

If you’re using your own view and form, please post them here.

1 Like

I have my own register view with:

def user_register(request):
    form = models.CreateUserForm()
    if request.method == "POST":
        form = models.CreateUserForm(request.POST)
        user = models.MyUser.objects.filter(email=request.POST['email'])
        if form.is_valid():
            if user.exists():
                form.add_error('email', 'email already exists on the system!')
            else:
                form.save()
                messages.success(
                    request, 'Your account has been successfully registered! Please check your email account in order to verify your email!')
    context = {
        'form': form,
    }
    return render(request, 'register.html', context)

in models.py:

class CreateUserForm(UserCreationForm):

    class Meta(UserCreationForm.Meta):
        model = MyUser
        fields = ['first_name',
                  'last_name', 'username', 'email', 'password1',  'password2']

urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('home', views.home_view, name="home"),
    path('', views.home_view, name="home"),
    path('register', views.user_register, name="register"),
    path('login', LoginView.as_view(
        template_name='login.html'), name="login"),
    path('send', views.send_view, name="send"),
    path('logout', views.user_logout, name="logout"),
    path('password_reset', views.password_reset, name="password_reset"),
]

The AUTH_USER_MODEL setting must be in your settings file, not in the app config. It’s a global (project-wide) setting, not limited to any individual part of the project.

See Customizing authentication in Django | Django documentation | Django

1 Like

If i set it in settings, i get the following:
“django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model ‘pages.MyUser’ that has not been installed”

even after replacing pages.apps.PagesAppConfig with ‘pages’ in INSTALLED_APPS.

I’ve tried clearing the migrations and cache but it comes up with this same error:

  File "D:\Django\python\mind\pages\models.py", line 4, in <module>
    from django.contrib.auth.forms import UserCreationForm
  File "D:\Django\python\mind\Lib\site-packages\django\contrib\auth\forms.py", line 18, in <module>
    UserModel = get_user_model()
                ^^^^^^^^^^^^^^^^
  File "D:\Django\python\mind\Lib\site-packages\django\contrib\auth\__init__.py", line 176, in get_user_model
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'pages.MyUser' that has not been installed

What was the exact line for the AUTH_USER_MODEL that you had tried in your settings.py file?

Also:

It looks like you’re trying to import UserCreationForm in your model. Why? I don’t see a reference to it in the portion of the models.py file you posted, and it’s generally not the right place to do this.

It’s on my previous reply:

class CreateUserForm(UserCreationForm):

    class Meta(UserCreationForm.Meta):
        model = MyUser
        fields = ['first_name',
                  'last_name', 'username', 'email', 'password1',  'password2']

It’s then used in the registration view. I did it to customize the fields and user model. The part of the user registration seems to be working fine. Should i set this under views?

Do you have the following line in your models.py file?:

If so, why?

Where are you referencing that class in your models.py? (And if you are, that is generally a mistake.)

1 Like

thanks i missed this, i should’ve added this to views.