Login

Compact idiom for legacy URLs in URLconfs

Author:
pbx
Posted:
April 20, 2007
Language:
Python
Version:
.96
Tags:
urlconf redirect
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. RegEx redirect fallback middleware by bowdengm 2 years, 12 months ago
  2. View Permission Decorator Helper by jgeewax 7 years ago
  3. Currency filter by kljensen 7 years, 5 months ago
  4. dropbox integration by ivarne 3 years, 3 months ago
  5. Resolve URLs to view name by UloPe 6 years, 3 months ago

Comments

Please login first before commenting.