×
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS PAYMENT INTEGRATION API BASICS Roadmap
     ❯   

CUSTOM USER MODEL

Handling Authentication

Handling Authentication with Custom User

Once you have created a custom User model, it’s essential to ensure that authentication works seamlessly with your modifications. This section will guide you through handling authentication processes, including login, logout, and user authentication checks.

Using Django's Built-in Authentication Views

Django provides built-in views to handle user authentication. You can utilize these views for logging in and logging out users without needing to create custom views from scratch.

Login View

To use the built-in login view, you need to set up a URL pattern in your urls.py:


from django.urls import path
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('login/', auth_views.LoginView.as_view(), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
]

Make sure to create corresponding templates for these views:

Creating the Login Template

Your login template (e.g., login.html) should extend from a base template and include a form for user credentials:

<form method="post">
    {% csrf_token %}
    <div>
        <label for="username">Username:</label>
        <input type="text" name="username" required>
    </div>
    <div>
        <label for="password">Password:</label>
        <input type="password" name="password" required>
    </div>
    <button type="submit">Log in</button>
</form>

Customizing Authentication Logic

If you need to customize the authentication logic (e.g., to include additional checks), you can override the default authenticate method in your custom User model manager:


from django.contrib.auth.models import BaseUserManager

class CustomUserManager(BaseUserManager):
    def create_user(self, username, password=None, **extra_fields):
        user = self.model(username=username, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        return self.create_user(username, password, **extra_fields)

Logging Out Users

To log users out, you can use the built-in logout view. Ensure that the logout URL is set up in your urls.py:


urlpatterns = [
    path('login/', auth_views.LoginView.as_view(), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
]

Implementing these built-in views allows you to handle user authentication with minimal effort, while still using your custom User model.


Django-tutorial.dev is dedicated to providing beginner-friendly tutorials on Django development. Examples are simplified to enhance readability and ease of learning. Tutorials, references, and examples are continuously reviewed to ensure accuracy, but we cannot guarantee complete correctness of all content. By using Django-tutorial.dev, you agree to have read and accepted our terms of use , cookie policy and privacy policy.

© 2024 Nischal Lamichhane. All Rights Reserved.
Django-tutorial.dev is styled using Bootstrap 5.
And W3.CSS.