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. Browser-native date input field by kytta 1 month, 1 week ago
  2. Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
  3. My firs Snippets by GutemaG 1 month, 2 weeks ago
  4. FileField having auto upload_to path by junaidmgithub 2 months, 3 weeks ago
  5. LazyPrimaryKeyRelatedField by LLyaudet 3 months ago

Comments

Please login first before commenting.