Implement user authentication system with custom user model, views, and templates
- 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.
This commit is contained in:
parent
432d2b7707
commit
faddc4c9b0
22 changed files with 556 additions and 24 deletions
73
accounts/views.py
Normal file
73
accounts/views.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from django.contrib.auth import login
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.views import LoginView, LogoutView
|
||||
from django.shortcuts import render
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic import CreateView
|
||||
|
||||
from accounts.forms import CustomUserCreationForm
|
||||
from accounts.models import User
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from django.forms import BaseModelForm
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
|
||||
|
||||
class CustomLoginView(LoginView):
|
||||
"""Custom login view with better styling."""
|
||||
|
||||
template_name = "accounts/login.html"
|
||||
redirect_authenticated_user = True
|
||||
|
||||
def get_success_url(self) -> str:
|
||||
"""Redirect to the dashboard after successful login.
|
||||
|
||||
Returns:
|
||||
str: URL to redirect to after successful login.
|
||||
"""
|
||||
return reverse_lazy("twitch:dashboard")
|
||||
|
||||
|
||||
class CustomLogoutView(LogoutView):
|
||||
"""Custom logout view."""
|
||||
|
||||
next_page = reverse_lazy("twitch:dashboard")
|
||||
|
||||
|
||||
class SignUpView(CreateView):
|
||||
"""User registration view."""
|
||||
|
||||
model = User
|
||||
form_class = CustomUserCreationForm
|
||||
template_name = "accounts/signup.html"
|
||||
success_url = reverse_lazy("twitch:dashboard")
|
||||
|
||||
def form_valid(self, form: BaseModelForm) -> HttpResponse:
|
||||
"""Login the user after successful registration.
|
||||
|
||||
Args:
|
||||
form: The validated user creation form.
|
||||
|
||||
Returns:
|
||||
HttpResponse: Response after successful form processing.
|
||||
"""
|
||||
response = super().form_valid(form)
|
||||
login(self.request, self.object) # type: ignore[attr-defined]
|
||||
return response
|
||||
|
||||
|
||||
@login_required
|
||||
def profile_view(request: HttpRequest) -> HttpResponse:
|
||||
"""User profile view.
|
||||
|
||||
Args:
|
||||
request: The HTTP request object.
|
||||
|
||||
Returns:
|
||||
HttpResponse: Rendered profile template.
|
||||
"""
|
||||
return render(request, "accounts/profile.html", {"user": request.user})
|
||||
Loading…
Add table
Add a link
Reference in a new issue