Handling legacy URLs with Django
One of the great things about Django is its simple and flexible URL handling. If your Django work, like mine, includes converting existing sites, you’ll probably be doing some URL cleanup along the way. Django’s “generic views” system includes a view called “redirect_to” that handles cases like this. A rule might look like this:
urlpatterns = patterns('django.views.generic.simple',
('^foo/oldsite/BadOldUrl33247.blech$', 'redirect_to', {'url': '/bar/nice-new-url/'}),
)
But because the URL pattern building happens in Python, if you have many of these you can do better than filling your urls.py with variants of that line. Here’s the root urlconf for one of my sites: