「スニペット」はDjangoの特定の要素を指しているのではなく、単に次のことを意味します。ここに使用するコードがあります。この場合、それはミドルウェアであり、Webリクエストの前後に呼び出される特定のDjangoモジュールです。必要に応じてdjangoのドキュメントを読む
私もこのミドルウェアを使用しています。メインのアプリケーションフォルダーで呼び出されるファイルにすべてを貼り付けるだけですmiddleware.py
(このアプリがで言及されている場合は、どのアプリフォルダーでもかまいませんINSTALLED_APPS
)
settings.py
次に、これらの行をファイルに追加します。
MIDDLEWARE_CLASSES = (
#...all others middleware, on the last line, paste :
'main.middleware.EnforceLoginMiddleware',
)
ここでファイルを置いたアプリはと呼ばれていることに注意してくださいmain
。あなたのアプリの名前は異なる場合があります。
スニペットのdocstringを読むことを忘れないでください:
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.