"snippets" does not point to a specific element of Django, it just means : here is a piece of code for you to use. In this case, it's a Middleware, a specific Django module that will be called before and after a web request. Read django docs if needed
I use this middleware too, just paste everything in a file called middleware.py
in your main application folder (any app folder will do, given this app is mentioned in INSTALLED_APPS
)
Then add these lines in your settings.py
file :
MIDDLEWARE_CLASSES = (
#...all others middleware, on the last line, paste :
'main.middleware.EnforceLoginMiddleware',
)
Note that here the app where I put the file is called main
, yours may be named differently.
Don't forget to read the docstring of the snippet :
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.