There are two modern ways to create a custom user model in Django: AbstractUser and AbstractBaseUser
-
update config/settings.py *
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'accounts', # new ] ... AUTH_USER_MODEL = 'accounts.CustomUser' # new
-
create a new CustomUser model *
class CustomUser(AbstractUser): pass # add additional fields in here def __str__(self): return self.username
-
create new UserCreation and UserChangeForm
class CustomUserCreationForm(UserCreationForm): class Meta: model = CustomUser fields = ('username', 'email') class CustomUserChangeForm(UserChangeForm): class Meta: model = CustomUser fields = ('username', 'email')
-
update the admin *
class CustomUserAdmin(UserAdmin): add_form = CustomUserCreationForm form = CustomUserChangeForm model = CustomUser list_display = ['email', 'username',] admin.site.register(CustomUser, CustomUserAdmin)
-
Insid app/urls.py add pathes: *
urlpatterns = [ path('', TemplateView.as_view(template_name='home.html'), name='home'), path('admin/', admin.site.urls), path('accounts/', include('accounts.urls')), path('accounts/', include('django.contrib.auth.urls')), ]
-
Create a urls.py file in the accounts app: *
urlpatterns = [ path('signup/', SignUpView.as_view(), name='signup'), ]
-
in views.py file in the accounts app *
class SignUpView(CreateView): form_class = CustomUserCreationForm success_url = reverse_lazy('login') template_name = 'registration/signup.html'
Now our registration page created