在 django 项目中放置 Markdown 扩展文件的正确位置?

发布于 2024-08-18 22:36:44 字数 254 浏览 5 评论 0原文

我已经为我正在开发的 django 项目创建了一个 markdown 扩展文件(称为 mdx_xxx.py),但我无法真正决定将其放在哪里。

文档说该文件应该驻留在 PYTHONPATH 上,我已经看到几篇博客文章邀请将文件放在项目的根目录中。

然而,这对我来说似乎是一个奇怪的地方,因为我宁愿在相关的应用程序目录中看到它,但它不再位于 PYTHONPATH 上。

一些经验丰富的 django 程序员能否解释一下这个问题?

谢谢

I've created a markdown extension file (called mdx_xxx.py) for a django project I'm working on but I can't really decide where to put it.

The documentation says that the file should reside on the PYTHONPATH and I've seen several blog posts inviting to just put the file in the root directory of the project.

However, that seems like an odd place to me as I would rather see it in the related application directory but then it's not on the PYTHONPATH anymore.

Could some experienced django programmer shed some light on this issue?

Thanks

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

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

发布评论

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

评论(3

与往事干杯 2024-08-25 22:36:44

要求扩展文件直接位于 Python 路径上,而不是位于任何包内,这是(IMO)Python markdown 实现的一个不幸的限制。

如果您的扩展非常适合您的项目,我认为将其放在项目根目录中是最好的选择。

另一方面,如果您的扩展在其他情况下可重用,我将使用简单的 setup.py 将其打包并使用 pip 将其安装到 virtualenv 中,就像我对所有其他依赖项所做的那样。

Requiring extension files to live directly on the Python path, and not inside any package, is (IMO) an unfortunate limitation of the Python markdown implementation.

If your extension is very specific to your project, I think putting it in the project root is the best option available.

On the other hand, if your extension is reusable in other cases, I would package it up with a simple setup.py and install it into a virtualenv using pip, like I do with all my other dependencies.

夏雨凉 2024-08-25 22:36:44

并非所有内容都应该包含在您的项目中。这是一个要求,一个依赖。您仍然可以将它们打包在一起,并且我认为您需要将其放在顶层。这基本上意味着可以从与项目本身相同的位置导入。就我个人而言,我将所有内容都推送到 virtualenv,因此它又漂亮又干净。如果您执行相同的操作,则部署过程应包括将项目和任何依赖项安全地放入该 virtualenv 中。否则,到路径中的任何位置。

Not everything should be in your project. This is a requirement, a dependency. You could still package them together, and you'll need to put this at the top level, I think. That basically means importable from the same location as the project itself. Personally, I push everything to a virtualenv, so its nice and clean. If you do the same, you're deployment process should include putting both your project and any dependencies safely into that virtualenv. Otherwise, to whatever location you have in path.

凑诗 2024-08-25 22:36:44

如果您现在使用 pip (pip install markdown) 版本 2.3.1 中的标准 Markdown 库,则扩展可以位于任何位置。您只需提供它的点路径即可。旧式 - 将其直接放在前缀为 mdx_ 的模块中的 PYTHONPATH 上仍然有效。

我的应用程序代码中有它:django_file_downloads.mdx_download。

要从 django 模板使用它:

{% load markup %}
{{ variable|markdown:'django_file_downloads.mdx_download' }}

If you are using standard markdown library from pip (pip install markdown) now at version 2.3.1, the extension can be anywhere. You just have to provide dotted path to it. The old-style - having it directly on the PYTHONPATH in module prefixed mdx_ still works as well.

I have it in my app code:django_file_downloads.mdx_download.

To use it from django templates:

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