Compact idiom for legacy URLs in URLconfs

 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}))

Comments

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.