"Snippets" zeigt nicht auf ein bestimmtes Element von Django, es bedeutet nur: Hier ist ein Stück Code für Sie zu verwenden. In diesem Fall ist es eine Middleware, ein spezifisches Django-Modul, das vor und nach einer Web-Anfrage aufgerufen wird. Read django docs benötigt, wenn
Ich benutze diese Middleware auch nur alles rief in einer Datei middleware.py
in die Hauptanwendung -Ordner (jede App-Ordner tun, da diese App in INSTALLED_APPS
erwähnt wird)
Dann fügen Sie diese Zeilen in Ihre settings.py
Datei:
MIDDLEWARE_CLASSES = (
#...all others middleware, on the last line, paste :
'main.middleware.EnforceLoginMiddleware',
)
Beachten Sie, dass hier die app, wo ich die Datei setzen main
genannt wird, kann bei Ihnen anders benannt werden.
Vergessen Sie nicht, das docstring des Snippets zu lesen:
Middlware class which requires the user to be authenticated for all urls except
those defined in PUBLIC_URLS in settings.py. PUBLIC_URLS should be a tuple of regular
expresssions for the urls you want anonymous users to have access to. If PUBLIC_URLS
is not defined, it falls back to LOGIN_URL or failing that '/accounts/login/'.
Requests for urls not matching PUBLIC_URLS get redirected to LOGIN_URL with next set
to original path of the unauthenticted request.
Any urls statically served by django are excluded from this check. To enforce the same
validation on these set SERVE_STATIC_TO_PUBLIC to False.
Vielen Dank! – Sevenearths