django共享库/类 [英] django shared library/classes
问题描述
是django的新手,正在寻找将共享库放置在何处的建议。我正在计划创建要在项目中所有应用程序中使用的类。放置它们的最佳位置在哪里?
Am new to django and was looking for advice where to place my shared library. Am planning on creating classes that I want to use across all my apps within the project. Where would be the best location to place them?
ex抽象模型
问候,
推荐答案
我们通常是这样设置项目的:
We usually set our projects up like this:
/site/
__init__.py
manage.py
settings.py
urls.py
/apps/
__init__.py
/appA/
__init__.py
/appB/
__init__.py
/lib/
__init__.py
/django-lib/
__init__.py
/shared-lib/
__init__.py
请确保您的网站目录位于python路径:
Just make sure your site directory is on your python path:
import sys
sys.path.append('/path/to/site/')
另外,请确保站点,应用程序和lib中存在init.py,以便将它们视为使用点符号导入的模块(导入site.lib.shared-lib)
Also, ensure that an init.py exists in site, apps, and lib so they can be treated as modules using dot notation imports (import site.lib.shared-lib)
编辑:
您有关python拍拍的问题h,这都与您的 manage.py或等效文件所在的位置有关。如果它位于/ site /目录(apps和lib旁边),则PYTHONPATH应该没问题。
In answer to your question regarding your python path, it all has to do with where your 'manage.py' or equivalent file resides. If it is under the /site/ directory (next to apps and lib) then the PYTHONPATH should be fine.
您需要确保每个目录都包含一个空文件称为 __ init __。py
。这告诉Python将目录视为模块。请参见上面的新的和改进的ASCII艺术。
You need to make sure that every directory contains an empty file called __init__.py
. This tells Python to treat that directory as a module. See the new and improved ASCII art above.
这篇关于django共享库/类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!