Chapter 8. Advanced Views and URLconfs

In Chapter 3, we explained the basics of Django view functions and URLconfs. This chapter goes into more detail about advanced functionality in those two pieces of the framework.

URLconf Tricks

There's nothing "special" about URLconfslike anything else in Django, they're just Python code. You can take advantage of this in several ways, as described in the sections that follow.

Streamlining Function Imports

Consider this URLconf, which builds on the example in Chapter 3:

from django.conf.urls.defaults import * from mysite.views import current_datetime, hours_ahead, hours_behind, now_in_chicago, now_in_london urlpatterns = patterns('', (r'^now/$', current_datetime), (r'^now/plus(\d{1,2})hours/$', hours_ahead), (r'^now/minus(\d{1,2})hours/$', ...

Get The Definitive Guide to Django: Web Development Done Right now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.