Defying Classification

by Malcolm Tredinnick

Topic: software/django/tips

Tue 12 Feb 2008

Django Tip: Application-level context processors

Posted at 14:53 +1100

Whilst going through my feed reading this morning, I read the application-level context processors were not possible. This came as a bit of a shock, since I've been using them for a while now. :-)

It's not hard and there's an interesting conclusion to be drawn about the power of scoping rules (plus a reprise on my thoughts on why putting everything under the sun into a framework isn't a great idea).

(Read more...)

Topics: software/django/tips

Sun 6 Jan 2008

Django Tip: Complex Forms

Posted at 13:39 +1100

On the Django mailing list, we periodically see requests for help by somebody trying to create a semi-complex form of some description. Often, getting a second perspective is a good choice because the original poster was overlooking a possibly easier alternate approach.

Today, I want to fill in one of the gaps. Something that is really easy once you know the machinery and possibly not so obvious until you sweat it out once: putting multiple repetitive sections into a single form.

(Read more...)

Topics: software/django/tips

Wed 14 Nov 2007

Django Tips: Source Code Filenames

Posted at 22:58 +1100

Following on from last week's post about writing code that isn't tied to a project structure in Django, here are a couple of other quick thoughts about file naming and layout.

(Apologies to the half dozen or so people who read this blog for the "anything but Django" content. I've been spending all my free time in the last week working on Django, so it's buzzing around in my head at the moment.)

(Read more...)

Topics: software/django/tips

Sun 11 Nov 2007

Django Tip: External Database Backends

Posted at 20:26 +1100

I was going to write a longer Django entry today about WSGI, but I've been spending time working on Django code instead, so a here's something shorter and possibly less useful that deserves some attention from Django users.

Every now and again, somebody has a need to change the behaviour in Django's existing database backends. Or they want to develop a new backed for, say, Firebird or Microsoft's SQL-Server or something. A couple of months ago (in revision 6316), George Vilches contributed a patch that gave Django the ability to import external database backends.

(Read more...)

Topics: software/django/tips

Fri 9 Nov 2007

Django Tip: Developing Without Projects

Posted at 22:03 +1100

As developers progress along the path from Django grasshopper to Django master and beyond, it slowly (or rapidly) dawns on them that Django's project concept is something that really only exists as a convenient kickstart mechanism. It's a great way to help you get your initial work started and arrive at the point where you can start writing code.

The usefulness of the project concept is reinforced by the fact that django-admin.py startproject exists and does all this work for you. Surely, we are meant to use projects all the time.

No! A project is certainly a convenient "getting started" concept. It's also a handy development aid (the meaning of which, I'll describe later). However, the core of Django-based software is the application, not the project. When you distribute Django software to other people, you are generally distributing one or more applications.

So, here are a few tips and tricks for using a collection of Django applications without needing to create a Django project.

(Read more...)

Topics: software/django/tips

Sun 7 Oct 2007

Django Tip: HTTP verb dispatching

Posted at 05:38 +1000

A recent thread on django-developers discussed one style of laying out views, whereby the handling of different request methods to the same URL end up in different functions. This isn't a requirement or even a good idea in some cases (since there's frequently a lot of common code), but when it is a good solution, doing it frequently can become repetitive.

Fortunately, adding any extra code to Django's core isn't necessary to handle this. We can write a helper method that's less than twenty lines of code (and could be much shorter if I wanted to be less readable). This article is about the design of such a feature (at least, about my design, since different people want different hings). Code is included.

(Read more...)

Topics: software/django/tips

Mon 26 Mar 2007

Django Tips: Variable Choice Lists

Posted at 14:22 +1000

Been a while since I added to this series. I've come across a couple of repeated questions lately, so it's time to give back to the knowledge pool again.

This time: using iterators to customise the options presented via the choices attribute on a model field.

(Read more...)

Topics: software/django/tips

Mon 3 Jul 2006

Django Tips: Forms With Multiple Inline Objects

Posted at 23:38 +1000 (edited 16 Sep 2006, 12:57)

The form creation and handling code in Django seems to strike a nice balance between "making the easy things easy and the hard things possible", as the saying goes. However, there are some areas of it that are not perfectly documented yet and sometimes you have to rummage through the source code for a few minutes to work how to achieve something that, in retrospect, is simple.

One example: how to create a form for editing multiple inline objects on a single page?

The admin interface manages this fairly nicely for relatively small numbers of objects. Surely it should be possible to do the same in our own, customised forms. It is indeed possible and not too difficult, either...

(Read more...)

Topics: software/django/tips, software/django/tutorials

Thu 29 Jun 2006

Django Tips: Extending Generic Views

Posted at 01:16 +1000 (edited 16 Sep 2006, 12:57)

James Bennett has started what I hope might be a series of Django tips today. Should become required reading for Django users, I suspect. So let's help the guy out a bit here. With any luck, we won't duplicate each other's work.

One thing that keeps coming up again and again on #django on IRC, the mailing list and in the Django bug tracker are requests for some small customisation to generic views to accommodate some particular use-case. In almost every case — I would say, in every case, but I am sure there are exceptions that I haven't seen yet — these cases can be handled with only a few lines of code and then passing off the bulk of the work to the generic views.

(Read more...)

Topics: software/django/tips, software/django/tutorials