- Created custom User model extending Django's AbstractUser. - Added user registration, login, and profile views with corresponding templates. - Implemented user authentication functionality and integrated Bootstrap for styling. - Updated project settings to use the new accounts app and user model. - Added tests for user model and admin functionality.
36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
from django.contrib.auth.forms import UserCreationForm
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from accounts.models import User
|
|
|
|
|
|
class CustomUserCreationForm(UserCreationForm):
|
|
"""Custom user creation form for the custom User model."""
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = ("username",)
|
|
|
|
def __init__(self, *args, **kwargs) -> None:
|
|
"""Initialize form with Bootstrap classes."""
|
|
super().__init__(*args, **kwargs)
|
|
# Add Bootstrap classes to form fields
|
|
for field in self.fields.values():
|
|
field.widget.attrs.update({"class": "form-control"})
|
|
|
|
def clean_username(self) -> str:
|
|
"""Validate the username using the correct User model.
|
|
|
|
Returns:
|
|
str: The cleaned username.
|
|
|
|
Raises:
|
|
ValidationError: If the username already exists.
|
|
"""
|
|
username = self.cleaned_data.get("username")
|
|
if username and User.objects.filter(username=username).exists():
|
|
msg = "A user with that username already exists."
|
|
raise ValidationError(msg)
|
|
return username or ""
|