Reputation: 1397
I'm starting on my first large django project and have realized that the default directory structure isn't going to work well.
I saw this question and answer and decided to implement something similar. Large Django application layout
What do I need to do to make something like this work? How can I change where django looks for apps/modules?
Upvotes: 1
Views: 644
Reputation: 109
if you add:
import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
to your settings .py and the following to your manage.py:
sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))
sys.path.insert(0, join(settings.PROJECT_ROOT, "lib"))
then you can include them in your installed apps just as you would if they were in your project root:
INSTALLED_APPS = (
#local apps
'myapp',
#local libs
'piston_dev',
)
this allows you a bit more freedom to move apps around without having to redeclare imports and such.
Upvotes: 1
Reputation: 10177
Python works automatically with deep directory structures. That's porbably you didn't find any instructions on how to do it.Here are some instructions on how to get classes and models to work.
If you want to have a module in folder yourproject/apps/firstapp
you can just add it to INSTALLED_APPS by adding line 'apps.firstapp',
. You will have to add a __init__.py
file to each of the directories so that they are recognized as python packages.
When you import classes you can simply use from yourproject.apps.firstapp.filename import yourclass
.
You should also make sure all template directories are listed in TEMPLATE_DIRS
.
Upvotes: 1
Reputation: 32070
I have two methods to handle this; one for production and one for development.
In development:
Append the path to your apps in your settings.py
. In my case, I have my main project in ~/hg/django/project/
and my apps are in ~/hg/django/apps/
. So I use:
if DEVEL:
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'apps'))
I also use the production method on my development box. This method has a knock on effect of letting me roll back to the most recent production version by simply commenting out the line path insertion in the settings.py
.
In production:
I install the apps using distutils
so that I can control deployment per server, and multiple projects running on each server can all access them. You can read about the distutils
setup scripts here. Then, to install, you simply:
./setup.py install
Upvotes: 1