在项目文件夹之外安装 Django Apps?
出于完全愚蠢的原因,我想彻底改变 Django 项目文件系统的布局方式。特别是,我想将组成网站的实际应用程序移动到与管理/设置文件不同的文件夹中。例如,而不是:
django/
site/
__init__.py
manage.py
settings.py
urls.py
home/
__init__.py
views.py
models.py
template.html
page/
__init__.py
views.py
models.py
template.html
media/
__init__.py
styles.css
picture1.jpg
picture2.jpg
我希望它是:
django/
site/
__init__.py
manage.py
settings.py
urls.py
home/
__init__.py
views.py
models.py
template.html
page/
__init__.py
views.py
models.py
template.html
media/
__init__.py
styles.css
picture1.jpg
picture2.jpg
因为我认为我的文件系统的 N 叉树尽可能平衡,最小化其高度以保证任何特定文件的 O(lgn) 搜索时间!
我已经成功地设置了 django.wsgi,以便当 python 运行时,所有“外部”文件夹都是系统路径的一部分,因此导入可以完美地工作。同样,我做了一个 os.chdir() 来使从文件系统加载也完美工作。 URL 映射器也可以完美工作,将传入请求定向到这些“外部”模块进行处理。
然而,我没有设法让manage.py加载这些“外部”模块,大概是因为它不通过django.wsgi运行,因此sys.path修改不适用于它们。因此,当我将这些外部文件夹放入 INSTALLED_APPS 中(例如“home”而不是“site.home”)时,它找不到它们,给我:
Error: No module named home
每当我尝试 python manage.pysyncdb 时。我尝试将 PYTHONPATH 环境变量设置为 /home/django,希望这会导致所有 python 脚本运行来检查该文件夹并加载我的“外部”模块,但这也失败了。我还能做些什么来让 manage.py 在加载其内容时检查这些外部文件夹吗?
For reasons of utter silliness, I want to completely change the way my Django project's filesystem is laid out. In particular, I want to move the actual apps making up the website into separate folders from the admin/settings files. For example, rather than:
django/
site/
__init__.py
manage.py
settings.py
urls.py
home/
__init__.py
views.py
models.py
template.html
page/
__init__.py
views.py
models.py
template.html
media/
__init__.py
styles.css
picture1.jpg
picture2.jpg
I want it to be:
django/
site/
__init__.py
manage.py
settings.py
urls.py
home/
__init__.py
views.py
models.py
template.html
page/
__init__.py
views.py
models.py
template.html
media/
__init__.py
styles.css
picture1.jpg
picture2.jpg
because i think the N-ary tree of my filesystem to be as balanced as possible, minimizing its height to guarantee O(lgn) search times for any particular file!!!
I have managed to set up my django.wsgi such that all my "external" folders are part of the system path when python runs, so imports work perfectly. Similarly, i did a os.chdir() to make loading-from-filesystem work perfectly too. The URL mapper also works perfectly, directing the incoming requests to these "external" modules to handle.
However, i did not managed to get manage.py to load these "external" modules, presumably because it doesn't run through django.wsgi and thus the sys.path modifications did not apply to them. Hence, when i put these external folders into INSTALLED_APPS (e.g. "home" rather than "site.home"), it cannot find them, giving me:
Error: No module named home
Whenever I try to python manage.py syncdb. I have tried setting the PYTHONPATH environmental variable to /home/django in the hope that that will cause any and every python script run to check that folder and load my "external" modules, but that failed too. Is there anything else i can do to get manage.py to check these external folders when it loads its stuff?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过这样的设置,您甚至可以将代码与 django 站点配置完全分离:
在 settings.py 中,只需将“src”目录加载到 PYTHONPATH 中:
最后,您安装的所有“可重用”应用程序都会作为 Egg 或pip 的工作方式相同,它们只是存在于 pythonpath 中。
With a setup like that, you could even completely separate your code from django site configuration:
And in the settings.py just load the "src" dir into the PYTHONPATH:
In the end, all the "reusable" apps you install as eggs or pips work the same way, they just live in the pythonpath.
我不能说我同意你的动机,但我相信问题仍然出在你的
PYTHONPATH
上。在你的 wsgi 脚本中尝试I can't say that I agree with your motivations, but I believe the problem is still with your
PYTHONPATH
. In your wsgi script try设置文件中的很多内容和/或都是相对于manage.py 编写的,至少我会将它们保留在根目录中。
So many things key off of the settings file and/or are written relative to manage.py that at the very least I'd keep those in the root directory.
如果您使用 setup.py 文件打包应用程序,则可以将它们“安装”到您的 virtualenv 中。这将使它们可用于该环境,但不可用于其他任何地方。
即使对于不可重用的应用程序,我也会这样做。部署过程的一部分是为我拥有的每个应用程序运行“pip install”(或者将应用程序推送到私有 pypi/cheeseshop 服务器)。开发使用“pip install -e”,这意味着我可以编辑和运行,而无需每次重新设置。
If you package up your apps with a setup.py file, you can 'install' them into your virtualenv. This will make them available for that environment, but not anywhere else.
I do this, even for non-reusable apps. Part of the deployment process is to run 'pip install ' for each app I have (or have the apps pushed to a private pypi/cheeseshop server). Development uses 'pip install -e ' which means I can edit and run without having to re-setup each time.