Have a problem with created new User model inherit form AbstractBaseUser - cannot add new user via my register template

Hi all,
I had created new user model inherited form AbstractBaseUser . I have changed My username field to ‘email’. I can create new user via admin panel but when i try to do this via register page it’s not working - i can add new user but cannot log in with new user. What i can see it’s when i create new user the password is not hashed in database.
I am new to Django and will appreciate every help.

models.py:

from django.db import models
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractBaseUser, AbstractUser, PermissionsMixin
# Create your models here

class ManagerProfile(BaseUserManager):
    def create_user(self,email, password= None):
        if not email:
            raise ValueError('podaj adres e-mail- jest wymagany')
        if not password:
            raise ValueError('ustal hasło- jest wymagane')
        email = self.normalize_email(email)
        
        user = self.model(email = email,username = email)
        user.set_password(password)
        user.save()
        return user
    def create_superuser(self,email, password= None):
        if not email:
            raise ValueError('podaj adres e-mail- jest wymagany')
        if not password:
            raise ValueError('ustal hasło- jest wymagane')
        user= self.create_user(email,password)
        user.is_superuser = True
        user.is_admin= True
        user.save()
        return user


class UserProfile(AbstractBaseUser, PermissionsMixin):
    user_id= models.AutoField(primary_key=True)
    email = models.EmailField(max_length=100, unique=True)
    username = models.CharField(max_length=100)
    is_admin= models.BooleanField(default= False)
    image= models.ImageField(upload_to = 'user_avatars/', blank=True)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    
    @property
    def is_staff(self):
        return self.is_admin
    objects = ManagerProfile()
    
    def __str__ (self):
        return self.email

forms.py

from django.forms import ModelForm, Textarea
from .models import UserProfile
from django import forms
import datetime
from django.forms.widgets import PasswordInput , EmailInput
from django.contrib.auth.forms import UserCreationForm


# Create the form class.

class UserForm(forms.Form):    
    
       email = forms.CharField(widget=forms.EmailInput(attrs={'placeholder': 'e-mail',  'class':'input_username'}))
       password = forms.EmailField(widget=forms.PasswordInput(
              attrs={'placeholder' :'hasło',  'class': 'input_username'}
              )
         )
       
class UserModelForm(ModelForm):
    class Meta:
        model = UserProfile
        fields = 'email' , 'password'

        widgets = {
            "password":PasswordInput(attrs={'placeholder' :'hasło',  'class': 'input_username'},render_value=True),
          
            "email": EmailInput(attrs={'placeholder': 'e-mail',  'class':'input_username'})
        }

views.py

from django.http import HttpResponseRedirect
from django.shortcuts import render ,redirect

from .forms import UserForm , UserModelForm
from .models import UserProfile
from django.utils import timezone
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm


def add_user(request):
   
    
    if request.method == "POST":
        form = UserModelForm(request.POST)

        if form.is_valid():
            email = form['email']
            password = form['password']
            username = email
            form.save()
            
           
            
        return redirect('edycja_danych')
        
    else:
        form = UserModelForm()

    return render(request, "register.html", {"form": form})   
``
register.html

{% extends “base.html” %}
{% load static %}
{% block title %}
Rejestracja Sparee
{% endblock title %}

{% block categories %}
{% endblock %}

{% block body %}

Stwórz konto w Sparee

Kontynuuj z Google

lub

{% csrf_token %} {{form}}
 <button id="add_account" type="submit">Utwórz konto</button>
  </form>
<p>Masz już konto w Sparee?</p>
<a href="#">Zaloguj się</a>
</div>
<div id=registration_info>
  <div id="reg_bilboard"><h3>Wypożyczaj od osób w swojej okolicy</h3>
    <div id="button_container">
      <button class="bilboard_button">Szybko</button>
      <button class="bilboard_button">Łatwo</button>
      <button class="bilboard_button">Bezpiecznie</button>
    </div>
  </div>
</div>

{% endblock body %}

login.html:

{% extends “base.html” %}

{% block categories %}
{% endblock %}

{% block body %}

Zaloguj się do Sparee

Kontynuuj z Google

lub

{%csrf_token%}

      <input type="checkbox" placeholder="" id="reg_check" value="Akceptuję postanowienia polityki prywatności, regulaminu oraz zasad odstąpienia od umowy w SPAREE."></input><br>
      <button id="add_account">Zaloguj</button><br><br><br>
 
  </form>
  

<a href="#">Odzyskiwanie hasła</a>
</div>
<div id=registration_info>
  <div id="reg_bilboard"><h3>Wypożyczaj od osób w swojej okolicy</h3>
    <div id="button_container">
      <button class="bilboard_button">Szybko</button>
      <button class="bilboard_button">Łatwo</button>
      <button class="bilboard_button">Bezpiecznie</button>
    </div>
  </div>
</div>

{% endblock body %}

Welcome @Sparee !

Correct.

This is not correct. You need to use the set_password method on the user object for the password to be hashed correctly.

Or, in your case, you should probably take advantage of the create_user method you have defined in your ManagerProfile class. If you’re not familiar with how that works, then you might want to look at the User model Admin class to see how it is used by the admin.

Thank You very much for Your hints :slight_smile: I’ve got it. Now it works .