guides - projectsteward-io/steward-learning GitHub Wiki
Summary: Implement Django Built-in Authentication (Login/Register) This step sets up user authentication in Django using its built-in authentication system.
- Add Authentication URLs Django provides built-in authentication views, so they are defined in urls.py:
python Copy Edit from django.contrib.auth.views import LoginView, LogoutView from django.urls import path from . import views # Import custom views if needed
urlpatterns = [ path('login/', LoginView.as_view(template_name='registration/login.html'), name='login'), path('logout/', LogoutView.as_view(next_page='login'), name='logout'), path('register/', views.register, name='register'), # Custom registration view ] /login/ loads Django’s LoginView using a custom login.html template. /logout/ logs users out and redirects them to the login page. /register/ handles user registration using a custom view. 2. Create the Login & Register Templates Django expects authentication templates inside a registration/ folder.
Create templates/registration/login.html html Copy Edit {% extends "base.html" %}
{% block content %}
{% csrf_token %} {{ form.as_p }} LoginDon't have an account? Register here
{% endblock %} Create templates/registration/register.html html Copy Edit {% extends "base.html" %}{% block content %}
{% csrf_token %} {{ form.as_p }} RegisterAlready have an account? Login here
{% endblock %} 3. Create the User Registration View Django provides a UserCreationForm, which is used in views.py:python Copy Edit from django.shortcuts import render, redirect from django.contrib.auth.forms import UserCreationForm from django.contrib import messages
def register(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): form.save() messages.success(request, "Your account has been created! You can now log in.") return redirect('login') else: form = UserCreationForm() return render(request, 'registration/register.html', {'form': form}) 4. Configure Redirects in settings.py Django needs to know where to redirect users after login and logout:
python Copy Edit LOGIN_REDIRECT_URL = '/' LOGOUT_REDIRECT_URL = '/login/' This means:
After login → Redirect to the homepage (/). After logout → Redirect to the login page (/login/). 5. Add a Logout Button to base.html A logout button should only appear when the user is logged in.
html Copy Edit
{% if user.is_authenticated %}Welcome, {{ user.username }}!
{% csrf_token %} Logout {% else %} Login Register {% endif %} 6. Test the Authentication System Go to /register/, create an account, and confirm registration works. Log in via /login/ and verify authentication is successful. After login, check that the logout button appears and works properly. Log out and confirm that you are redirected back to /login/. Summary of What This Step Achieves Uses Django’s built-in authentication system for simplicity. Provides Login, Logout, and Register functionality. Uses Django's UserCreationForm to handle user registration. Ensures users are redirected properly after login/logout. Implements a logout button that only appears when a user is logged in.