安装片段

发布于 2024-10-12 09:16:19 字数 378 浏览 3 评论 0原文

如何在 django 中安装代码片段? (具体来说这个

我有文件/{project}/snippets/EnforceLoginMiddleware.py< /code> 并且我在 MIDDLEWARE_CLASSES 中尝试了任意数量的排列来加载它以及谷歌搜索 django snippets install 但无济于事:(

任何帮助将不胜感激:)

< em>PS(为什么我找不到任何有关安装片段的文档或示例。也许我只是一个糟糕的googler)

How do I install snippets in django? (specifically this)

I have the file /{project}/snippets/EnforceLoginMiddleware.py and I have tried any number of permutations inside MIDDLEWARE_CLASSES to load it as well as googling django snippets install to no avail :(

Any help would be grateful :)

PS(Why can't I find any documentation or examples on the installation of snippets. Maybe I'm just a bad googler)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷了相思 2024-10-19 09:16:19

“snippets”并不指向 Django 的特定元素,它只是意味着:这里有一段代码供您使用。在本例中,它是一个中间件,一个特定的 Django 模块,将在 Web 请求之前和之后调用。 阅读 django 文档 如果需要

我也使用这个中间件,只需粘贴所有内容在主应用程序文件夹中名为 middleware.py 的文件中(任何应用程序文件夹都可以,因为 INSTALLED_APPS 中提到了此应用程序),

然后将这些行添加到您的 settings.py 文件:

MIDDLEWARE_CLASSES = (
    #...all others middleware, on the last line, paste :
    'main.middleware.EnforceLoginMiddleware',
)

请注意,这里我放置文件的应用程序称为 main,您的应用程序的名称可能不同。

不要忘记阅读代码片段的文档字符串:

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.

"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.
年少掌心 2024-10-19 09:16:19

您需要确保您的代码片段位于 PYTHONPATH (sys.path) 上,或者该代码片段位于 PYTHONPATH 上存在的模块内。

在这种情况下,如果您将 __init__.py 文件添加到您的 snippets 文件夹中,则会将 snippets 文件夹视为一个模块,然后您可以执行 from snippets.EnforceLoginMiddleware import EnforceLoginMiddleware代码>.我认为这是您缺少的关键步骤。

您的中间件类将如下所示:

MIDDLEWARE_CLASSES = (..., 'snippets.EnforceLoginMiddleware.EnforceLoginMiddleware')

You need to make sure your snippet is on your PYTHONPATH (sys.path) or, that the snippet is within a module that exists on your PYTHONPATH.

In this case, if you add an __init__.py file to your snippets folder, that will treat the snippets folder as a module, and then you can do from snippets.EnforceLoginMiddleware import EnforceLoginMiddleware. I think this is the crucial step you're missing.

Your middleware classes would then look like:

MIDDLEWARE_CLASSES = (..., 'snippets.EnforceLoginMiddleware.EnforceLoginMiddleware')
土豪 2024-10-19 09:16:19

在您的示例中,MIDDLEWARE_CLASSES 的新条目必须类似于“{project}.snippets.EnforceLoginMiddleware.EnforceLoginMiddleware”(替换为您的项目的包名称)。

确保您的代码片段文件夹也有一个 __init__.py 文件!

PS:这可能是因为 Django 中的官方组件不存在“片段”这样的东西。它们通常是简单的 Python 代码片段,必须如此对待。 Djangosnippets 只是一个可以发布和共享 Django 代码(也称为代码片段)的网站。

In your example your new entry to MIDDLEWARE_CLASSES would have to look like '{project}.snippets.EnforceLoginMiddleware.EnforceLoginMiddleware' (replace with the package name for your project).

Make sure, that your snippets folder also has an __init__.py file!

P.S.: that's probably because there is no such thing as a "snippet" when it comes to official components in Django. They usually are simple Python code fragments and have to be treated as such. Djangosnippets is just a site where you can post and share django code (also called snippets).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文