The correct place to put a markdown extension file in a django project?
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 开发者_JAVA技巧programmer shed some light on this issue?
Thanks
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.
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.
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' }}
精彩评论