安装片段
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“snippets”并不指向 Django 的特定元素,它只是意味着:这里有一段代码供您使用。在本例中,它是一个中间件,一个特定的 Django 模块,将在 Web 请求之前和之后调用。 阅读 django 文档 如果需要
我也使用这个中间件,只需粘贴所有内容在主应用程序文件夹中名为
middleware.py
的文件中(任何应用程序文件夹都可以,因为INSTALLED_APPS
中提到了此应用程序),然后将这些行添加到您的
settings.py
文件:请注意,这里我放置文件的应用程序称为
main
,您的应用程序的名称可能不同。不要忘记阅读代码片段的文档字符串:
"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 inINSTALLED_APPS
)Then add these lines in your
settings.py
file :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 :
您需要确保您的代码片段位于 PYTHONPATH (sys.path) 上,或者该代码片段位于 PYTHONPATH 上存在的模块内。
在这种情况下,如果您将
__init__.py
文件添加到您的 snippets 文件夹中,则会将 snippets 文件夹视为一个模块,然后您可以执行from snippets.EnforceLoginMiddleware import 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 dofrom snippets.EnforceLoginMiddleware import EnforceLoginMiddleware
. I think this is the crucial step you're missing.Your middleware classes would then look like:
在您的示例中,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).