如何将所有django应用程序保存在特定文件夹中 [英] How to keep all my django applications in specific folder

查看:813
本文介绍了如何将所有django应用程序保存在特定文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django项目,让我们说project1。
应用程序的典型文件夹结构是:

  / project1 / 
/ app1 /
/ app2 /
...
__init__.py
manage.py
settings.py
urls.py

如果我想把所有的应用程序放在一些单独的文件夹中,应该怎么办?所以结构应该如下所示:

  / project / 
apps /
app1 /
app2 /
...
__init__.py
manage.py
settings.py
urls.py


解决方案

您可以将应用程序文件夹添加到您的python在 settings.py 中插入以下内容的路径:

  import os 
import sys

PROJECT_ROOT = os.path.dirname(__ file__)
sys.path.insert(0,os.path.join(PROJECT_ROOT,'apps'))

然后,您可以使用该文件夹中的所有应用程序,就像在项目根目录中一样!


I have a Django project, let's say "project1". Typical folder structure for applications is:

/project1/
         /app1/
         /app2/
         ...
         __init__.py
         manage.py
         settings.py
         urls.py

What should I do if I want to hold all of my applications in some separate folder, 'apps' for example? So that structure should look like the following:

/project/
         apps/
              app1/
              app2/
              ...
         __init__.py
         manage.py
         settings.py
         urls.py

解决方案

You can add your apps folder to your python path by inserting the following in your settings.py:

import os
import sys

PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, 'apps'))

Then you can use all the apps in this folder just in the same way as they were in your project root!

这篇关于如何将所有django应用程序保存在特定文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆