Published at Sept. 24, 2012 | Tagged with: Python , Django , Development

As I promised in Automated deployment with Ubuntu, Fabric and Django I will use this post to explain the file structure that I use for my Django projects and what I benefit from it. So here is my project directory tree.

The structure

~/workspace/<project_name>/ |-- bin |-- include |-- lib |-- local |-- src |-- .git |-- .gitignore |-- required_packages.txt |-- media |-- static |-- <project_name> | |-- <project_name> | | |-- __init__.py | | |-- settings | | | |-- __init__.py | | | |-- <environment_type>.py | | | |-- local.py | | |-- templates | | |-- urls.py | | |-- views.py | |-- manage.py | |-- wsgi.py |-- <project_name>.development.nginx.local.conf |-- <project_name>.< environment_type>.nginx.uwsgi.conf |-- <project_name>.< environment_type>.uwsgi.conf

Explanation

At the top I have a directory named as the project and virtual environment inside of it. The benefit from it is complete isolation of the project from the surrounding projects and python packages installed at OS level and ability to install packages without administrator permissions. It also provides an easy way to transfer the project from one system to another using a requirements file.

The src folder is where I keep everything that is going to enter the version control project source, requirements files, web server configuration etc.

My default .gitignore is made to skip the pyc-files, the PyDev files and everything in the static and media directories.

The media directory is where the MEDIA_ROOT settings point to, respectively static is for the STATIC_ROOT.

All required packages with their version are placed in required_packages.txt so we can install/update them with a single command in the virtual environment.

In a directory with the name of the project is where the python code resides. Inside it the project structure partly follows the new project layout introduced in Django 1.4.

The big difference here is the settings part. It is moved as a separate module where all common/general settings are place in __init__.py

Benefits

Complete isolation from other projects using virtual environment

Easy to transfer and update packages on other machines thanks to the pip requirements file

Media/static files outside the source directory for higher security

Web server/uWSGI configuration as part of the repository for easier and error proof synchronization

Probably it have some downside(I can not think any of these now but you never know) so if you think that this can be improved feel free to share your thoughts. Also if there is anything not clear enough, just ask me, I will be happy to clear it.