Django Tutorial 6 - User Authentication Part 1 - Hacked Existence Page
from django.contrib.auth.decorators import login_required from django.shortcuts import render @login_required def secret_page(request): return render(request, 'secret.html') Use code with caution. Copied to clipboard
You can restrict access to certain views so that only logged-in users can see them. Use the @login_required decorator for function-based views. Use the LoginRequiredMixin for class-based views.
This guide covers the core concepts of setting up user authentication based on the Hacked Existence tutorial series. 🚀 Core Authentication Concepts from django
INSTALLED_APPS = [ 'django.contrib.auth', 'django.contrib.contenttypes', # ... other apps ] Use code with caution. Copied to clipboard 📝 Step 2: Set Up the Login View
from django.contrib.auth import views as auth_views from django.urls import path urlpatterns = [ path('login/', auth_views.LoginView.as_view(), name='login'), ] Use code with caution. Copied to clipboard 2. Create the Login Template Use the LoginRequiredMixin for class-based views
Add LOGOUT_REDIRECT_URL = 'login' to redirect users to the login page after logging out. 🛡️ Step 4: Protect Your Views
After a successful login, Django needs to know where to send the user. You can define this in your settings.py file. other apps ] Use code with caution
Django enables the authentication system by default in new projects. Open your settings.py file and ensure the following apps are listed in INSTALLED_APPS :