Home > database >  Cant login to created user after using custom model
Cant login to created user after using custom model

Time:03-01

from django.db import models
from django.contrib.auth.models import AbstractUser,User
from django.utils.html import escape,mark_safe
# Create your models here.
class user(AbstractUser):
    is_admin = models.BooleanField(default=False)
    is_teacher = models.BooleanField(default=False)
    is_student = models.BooleanField(default=False)


    def login_view(request):
form = AuthenticationForm()
if request.method=='POST':
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(request, username=username,password=password)
    if user.is_admin==True:
        login(request,user)
        return HttpResponse(f"Welcome {username}")
    else:
        return HttpResponse('failled')

return render(request, 'login.html', {'form': form})

cant login using the user created by superuser. the user is creating but while calling it or using it shows

created a user with staff and superuser authorization still cant login

CodePudding user response:

I think you need to set the setting AUTH_USER_MODEL. Also, not sure why you're overriding the login view. You can use another setting LOGIN_REDIRECT_URL to set the URL where the user gets redirected on successful login.

CodePudding user response:

You need to unregister the built-in User from the admin and then register your own user model in admin.py

Example in the docs

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

from customauth.models import user


admin.site.register(user, UserAdmin)
# admin.site.unregister(User)

You may have to subclass the UserChangeForm to add your custom fields

  • Related