September archive

Four new shortcuts

September 24, 2005

We've added four new shortcuts that'll help you use even less code in your Django applications. Each shortcut is designed to express a common idiom in a single line of code.

The first idiom is something like this:

from django.core import template, template_loader
from django.utils.httpwrappers import HttpResponse
def foo_view(request):
    t = template_loader.get_template('foo/foo_detail')
    c = template.Context({'foo': 'bar'})
    return HttpResponse(t.render(c))

If you're a Django developer, you've probably used something very similar to that: Load a template, fill it with a context and return an HttpResponse with the rendered template. Because that's so common, we've added a shortcut: render_to_response(). Here's the same code, rewritten:

from django.core.extensions import render_to_response
def foo_view(request):
    return render_to_response('foo/foo_detail', {'foo': 'bar'})

We've also added render_to_string(), which does the same thing as render_to_response() but returns a string instead of an HttpResponse.

Here's a second idiom that's quite common in Django code:

from django.core.exceptions import Http404
from django.models.bar import foos
    f = foos.get_object(some_field__exact=some_lookup)
except foos.FooDoesNotExist:
    raise Http404

We've introduced get_object_or_404() to reduce this common case to a single line. Here's the same code, rewritten:

from django.core.extensions import get_object_or_404
from django.models.bar import foos
f = get_object_or_404(foos, some_field__exact=some_lookup)

There's also a get_list_or_404(), which works the same way but uses get_list() instead of get_object().

We've updated the documentation to use this new method in examples.

Finally, in both of these examples, old code (writing things out the long way) will still work.


September 17, 2005

As Simon pointed out, the list of Django-powered sites is growing by the day.

In recognition of the growing community, and in the interest of promoting even more growth, I've made a set of "official" Django badges. If your site or project is powered by Django, you can help promote the growing community by slapping one of them on your site with a link back to djangoproject.com.

They come in grey and green and all kinds of sizes, so grab a Django badge for your site today and let the world know what you're made of.

More docs: Customizing the template language

September 5, 2005

An oft-heard request in the Django community is: "How can I write custom template tags?" Now, finally, there's an answer.

I've added a document called The Django template language: For Python programmers. It gets into the Python nitty-gritty of the template system, as opposed to the other template doc, The Django template language: For template authors, which focuses on template use from a (X)HTML hacker's perspective.

The new doc explains how to write custom template filters and template tags. It also goes over the basics of how you should use the template system in your code, so it's a must-read for every Django developer.

More docs!

September 1, 2005

I've added two more pieces of documentation today, both of which describe core Django functionality.