Andy McKay

Sep 03, 2009

First things to do on a Django project...

Call them best practices if you like, but here's the things I do when starting a new Django project.

  • Create an "app" app. The app folder is the catch all for all the non-reusable parts of your site. The bit that actually does alot of the site specific stuff. Usually, but not always, it only contains an (this is so that Django knows its an app).
  • Create a templates folder. Inside the app project, this is for all the site specific stuff, as opposed to hopefully reusable parts in the specific folders.
  • Create a template context processor. You know you will need one eventually, every project does. Don't forget to hook it into the settings file. Mine is normally called and placed in the app project. It does absolutely nothing at this point, it's just waiting to be customised.
  • Create a middleware file. You might not need it, but chances are you will. Don't forget to hook it into the settings file. Mine is normally called and placed in the app project. Again it does absolutely nothing at this point.
  • (Optional) Create a custom authentication backend. Called and placed in the app project. Hook it up when you need it.

So that's the app project. Next let's set up the static content, that's easy:

  • Create a "static" folder. This is where the css, js, img will live.
  • Serve static through the dev server. When it goes live, I leave the content and urls in the same place, but have static overriden at the Apache (or your favourite server) level to serve the static content and not your dev server.

Create a restart script that starts everything cleanly. For me this normally means:

  • drops the database
  • recreates it
  • syncs the database (syncdb), without input
  • creates a superuser, or loads in a fixture with that data
  • loads in the required fixtures

Here's an old one that does that, I've changed the one I use a bit since then, will need to do a new post. Whilst you are quickly iterating this is the best way to go, it keeps it clean and simple. You'll want to use South or django-evolution once it goes live of course.

For each subsequent app (things that have the models that actually do the work) that you create:
  • Create a forms folder add an inside it. Forms will live here.
  • Delete and create a models folder. Add an inside it. In the import each of the modules that you will be adding. Don't forget to add in app_label on your models.
  • Inside the models folder, create a fixtures folder. Guess what goes in here.
  • Create a Signal code will go here.
  • Delete and create a views folder.
  • Create a tests folder, add an inside it. Tests will live here.
  • Create a template folder. This is where the templates live.
And that's about it. Next to do on the list is to create a paster script that does all this so you don't have to.