Django- how can login page with Custom User without UserCreationForm?

models.py

class CustomUser(AbstractUser): is_student = models.BooleanField(default=False) is_teacher = models.BooleanField(default=False) class palabout(models.Model): user = models.ForeignKey(CustomUser, blank=True, null=True, on_delete=models.SET_NULL) profileImage = models.FileField() username = models.CharField(max_length=30) email = models.EmailField(max_length=100) password = models.CharField(max_length=100) fname = models.CharField(max_length=30) lname = models.CharField(max_length=30) gender = models.CharField( max_length=1, choices=(('m', ('Male')), ('f', ('Female'))), blank=True, null=True) dob = models.DateField(max_length=8)

forms.py

class pal1Form(forms.ModelForm):
    password=forms.CharField(widget=forms.PasswordInput())
    class Meta():
        model=CustomUser
        fields=['username','email','password']

def save(self):
    user = super().save(commit=False)
    user.is_student = True
    user.save()
    return user
class palForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)
    class Meta:
        model=palabout
        fields =['username','password','email','fname','lname','dob','gender','profileImage']

views.py

from .forms import palForm,pal1Form 
def add_form(request):
        form = palForm(request.POST, request.FILES)
        form1=pal1Form(request.POST)
        if request.method == "POST":
           form = palForm(request.POST  , request.FILES)
           form1=pal1Form(request.POST)
        if form.is_valid() and form1.is_valid() :
          try:
            form.save()
            form1.save()
            messages.success(request,"Successfully Added")
            return render(request,"home.html")
          except:
            messages.error(request,"Failed to Add")
            return render(request,"home/pal-form.html",context={"form":form})
        else:
            form=palForm()
            form=pal1Form()
            return render (request,"home/pal-form.html",context={"form":form,"form1":form1})

def login(request):
    form=pal1Form(request.POST)
    if request.method == 'POST':
        form=pal1Form(request.POST)
        if form.is_valid():
            user = authenticate(
                username=form.cleaned_data['username'],
                password=form.cleaned_data['password'],
            )
            if user is not None:
                login(request, user)
                return redirect("menu")
            else:
                messages.error(request,"Invalid username or password.")
                return redirect(login")
    return render(
        request, 'login.html', context={'form': form})

I'm trying a lot login page with multiple user page but login is not open so How can i login in custom user page? What was the problem? Can anyone help me?

Back to Top