Login

Compact idiom for legacy URLs in URLconfs

Author:
pbx
Posted:
April 20, 2007
Language:
Python
Version:
.96
Score:
6 (after 6 ratings)

Taken from the longer description on my blog:

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...

Just plug your old/new URL pairs into redirects.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from django.views.generic.simple import redirect_to

urlpatterns = patterns('',
    # ...your project's patterns...
    )

# Your old/new URL tuples -- examples
redirects = (
    ('^kawasaki/zx750f', '/bikes/kawasaki/zx750f/'),   # Matching a literal URL
    ('^gear/[0-9]+/$', '/gear/'),                      # Ignoring part of the matched URL
    ('^foo/(?P<id>\d+)/$', '/bar/%(id)s/'),            # Oooh, parameter capture!
    )

for oldurl, newurl in redirects:
    urlpatterns += patterns('', (oldurl, redirect_to, {'url': newurl}))

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.