Monday, January 14, 2013

Django projects at a glance

I have decided to write this after experiencing an extreme amount of frustration at the Django web application framework for Python. Some things are simply not clearly spelled out in the docs.

Django project structure: When possible, make any model a separate app

Any Django project should be a collection of apps. Now, if you're a normal human, you may think of "Chrome" as an app, and maybe "iTunes" and "Microsoft Word" and things like that. However, in Django, app is something on a much smaller scale. If you have blog posts and comments, that's a whole app. That app is separate from a blog authors app. Essentially, you want to break a single web project into as many small apps as you can, even if the apps that make up your site are co-dependent and not usable on their own.

One of the largest benefits to splitting your project into as many apps as possible is it keeps the models.py files a sane size. There is simply no straightforward, supported way to break up models.py. I tried to create a models/ directory with an __init__.py that had `from x import *` for each model module x in my project. This unfortunately lead to my Django fixtures breaking, and as a consequence, half of my tests failing.

Templates and static files go in the app directory

This will help keep all your static files logically separated. Single-use Javascript widgets will get to go in the folder closer to their actual use, while broadly used files go into a central location.

Given your Django app is called 'app': Templates go into app/templates/app. Static files go into app/static/app. The reason you make a new app folder in templates/ and static/ is that Django doesn't split up different app files into different namespaces in any way. Maybe (hopefully) this will be added in a future version of Django, but for now, you must create a root folder for you static files and templates to prevent filename collision with other apps.

As for those global-use files: an ideal central location is not yet known to me. I am still in the process of restructuring my Django project. However, I have a hunch that I'd put project-global files in the the static/ and templates/ directory in the main app (the app with settings.py). I have not yet attempted yet.

More details

For more detail, go to this StackOverflow question. I stumbled upon it just now when trying to figure out