I’ve been developing in Django a fair bit lately. For the most part, I love django. One thing that really bugs me though, is the way the templates provided don’t actually set up sensible, working defaults for you though. To get to the stage of actually creating something with Django, you first have to:

  • Create a project (django-admin startproject aproject)
  • Configure the database for development (edit ./settings.py, set database type to sqlite, set filename to dev_db.sqlite or similar)
  • Enable the administration interface (by editing settings.py and specifying a list of additional modules to load, along with the urls that module should be applied to)
  • Create an app (./manage.py startproject firstapp)
  • Load the app by again editing the modules list in settings.py
  • Edit the urls.py file to use the submodule’s urls.py
  • Create the models in the app
  • Create a view in the app
  • Create a urls.py file in the app, which uses the view
  • Create a global template directory and a base.html template there
  • Create a template directory hierarchy for the app
  • Create a template in the app’s hierarchy, which inherits from the base.html template
  • Modify the settings.py file to load the sub-app’s templates

Actually, this is an incomplete list, if you want a truly pluggable app, more than three of these steps would need to be repeated for additional apps. Ideally, a fully pluggable app would just work, if created, and a view, url, and template was edited within it.

I find this all very tedious, irrational, wasteful, and prone to error. Strangely, it violates django’s self-proclaimed main principle: DRY: Don’t Repeat Yourself!

As a result, I’ve been using both a project and an application template which I store in Git, which already has all of these things setup. I clone my django project template (instead of using django-admin startproject) to create new projects, and clone the app (instead of using ./manage.py startapp) to create an app inside the project. By using git’s submodule feature, I can develop an app in one project, but keep it’s history separate, so it’s easily reusable in other projects later.

I also include some submodules in my templates by default. So the idea is that I can just do:

# create a project, and cd into it
git clone TEMPLATES/django-project newproj
cd newproj

# create a new app within the project
git clone ../TEMPLATES/django-app app1
cd app1

# edit the models
vim models.py

# edit the default view (index) – urls.py is already there,
# and will be automatically plugged into the project’s urls.py
vim views.py

# edit the template for the index view
vim Templates/app1/index.html

# initialise the db, and run the server
cd ../..
./manage.py syncdb
./manage.py runserver_plus

One issue remains, which prevents true pluggability: linking apps requires detailed knowledge of their urls, and breaks flexible layout, etc. Instead, I’m planning to look into django’s event system, as a way of sending messages to any other apps who care to listen, without needing to hard-code the listener’s URLs into the dispatcher. But that’s a little further down the road, when I have more time.

Tags: , , , , , , ,

3 Responses to “Pluggable Django: Git templates for turnkey projects and apps”

  1. Doug Ireton says:

    Thanks for this post. I’m just getting started with Django and the setup is tedious and error prone. I completely agree that Startproject and startapp don’t do enough.

    Any chance you’ll be sharing your project and app templates on GitHub?

    Thanks,

    Doug

    • Lee says:

      @Doug: The plan was to share them on my existing gitweb site (git.irukado.org). I don’t like the commercial nature of github. However, if a lot of people start using it, that could be trouble, and github might be a better choice. I’ll give the repo location some more thought and maybe try a few stress tests before releasing :)

  2. gcallaghan says:

    I agree, definitely one of my pet peeves as well.

    P.S. I see your a vim user. Have you checked out eclim?

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">