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

More like this

  1. Currency filter by kljensen 4 years, 4 months ago
  2. View Permission Decorator Helper by jgeewax 3 years, 10 months ago
  3. Simple Age Verification Middleware by eculver 2 years, 9 months ago
  4. login_required decorator that doesn't redirect by brutasse 1 year, 3 months ago
  5. ThumbnailMixIn by johan 3 years, 1 month ago

Comments

(Forgotten your password?)