django-auth-tutorial icon indicating copy to clipboard operation
django-auth-tutorial copied to clipboard

`login_not_required` for Django 5.1+ with `django.contrib.auth.middleware.LoginRequiredMiddleware`

Open sierra-alpha opened this issue 11 months ago • 0 comments

Hi there, great tutorial, thanks so much it helped me out a lot!

One things that tripped me up was that I was using Django 5.1 with the login required turned on by default, it was a simple fix though to use the login_not_required decorator.

So https://github.com/wsvincent/django-auth-tutorial/blob/main/django_project/urls.py becomes:

from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
from django.contrib.auth.decorators import login_not_required


urlpatterns = [
    path("admin/", admin.site.urls),
    path("accounts/", include("accounts.urls")),  # new
    path("accounts/", include("django.contrib.auth.urls")),
    path("", login_not_required(TemplateView.as_view(template_name="home.html")), name="home"),
]

and https://github.com/wsvincent/django-auth-tutorial/blob/main/accounts/urls.py becomes:

from django.urls import path
from django.contrib.auth.decorators import login_not_required

from .views import SignUpView


urlpatterns = [
    path("signup/", login_not_required(SignUpView.as_view()), name="signup"),
]

Sorry I don't have time for a PR but you'd need to also bump the requirements.txt to Django 5.1 and add the "django.contrib.auth.middleware.LoginRequiredMiddleware" to the MIDDLEWARE =[...] section in the Django projects settings.py if you wanted to incorporate this into the tutorial.

sierra-alpha avatar Dec 28 '24 21:12 sierra-alpha