Locating Django Applications in Their Own Sub-Directory
Posted by Paul on 17th February 2014
It’s straightforward to keep Django applications in their own directory if necessary – though the convention is to place them in the project’s root directory.
Instructions below assume pwd/$PWD is the project’s root directory.
- Create the directory in which Django applications will be located:
$ mkdir apps
- Create a new application named _newapp_ under
apps/newappusingstartapp‘s optional directory parameter (this requires that the target directory exist, so create it first):
$ mkdir apps/newapp $ ./manage.py startapp newapp apps/newapp
- Modify the project’s
manage.pyfile to ensure the development server can find the applications. It should end up looking something like the following:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
# Add the apps directoriy to Python's path. In production it will
# be necessary to add the apps directory to the path, too.
from os.path import abspath, dirname, join
PROJECT_ROOT = abspath(dirname(__file__))
sys.path.append(join(PROJECT_ROOT, "apps"))
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
- For a development environment, that’s it. For production, ensure the
appsdirectory is included on the project’sPYTHONPATH. If using Apache andmod_wsgithen ensure that theWSGIDaemonProcessdirective in the project’s<virtualhost/>includes the path to the newly addedappsdirectory, as well as the path to the (virtualenv’s) python site packages directory and the project’s root directory:
WSGIDaemonProcess myapp python-path=/myapp/apps:/myapp/python-packages:/myapp