Sev*_*ths 2 django django-middleware django-settings
如何在django中安装片段?(特别是这个)
我有文件/{project}/snippets/EnforceLoginMiddleware.py,我已经尝试了任何数量的排列MIDDLEWARE_CLASSES加载它以及谷歌搜索django snippets install无济于事:(
任何帮助将不胜感激:)
PS(为什么我找不到任何关于安装代码片段的文档或示例.也许我只是一个糟糕的Google员工)
"snippets"并没有指向Django的特定元素,它只是意味着:这里有一段代码供你使用.在这种情况下,它是一个中间件,一个特定的Django模块,将在Web请求之前和之后调用.如果需要,请阅读django文档
我也使用这个中间件,只需将所有内容粘贴到middleware.py主应用程序文件夹中调用的文件中(任何应用程序文件夹都会执行,因为提到了这个应用程序INSTALLED_APPS)
然后在settings.py文件中添加以下行:
MIDDLEWARE_CLASSES = (
#...all others middleware, on the last line, paste :
'main.middleware.EnforceLoginMiddleware',
)
Run Code Online (Sandbox Code Playgroud)
请注意,这里调用我放置文件的应用程序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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1283 次 |
| 最近记录: |