Using reverse() to do redirects

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from django.core.urlresolvers import reverse
from django.http import HttpResponsePermanentRedirect


MONTH_DICT = { '01': 'jan',
               '02': 'feb',
               '03': 'mar',
               '04': 'apr',
               '05': 'may',
               '06': 'jun',
               '07': 'jul',
               '08': 'aug',
               '09': 'sep',
               '10': 'oct',
               '11': 'nov',
               '12': 'dec' }


def redirect_detail(request, year, month, day, slug, object_type):
    return HttpResponsePermanentRedirect(reverse('coltrane_%s_detail' % object_type,
                                                 kwargs={ 'year': year,
                                                          'month': MONTH_DICT[month],
                                                          'day': day,
                                                          'slug': slug }))


def redirect_archive_day(request, year, month, day, object_type):
    return HttpResponsePermanentRedirect(reverse('coltrane_%s_archive_day' % object_type,
                                                 kwargs={ 'year': year,
                                                          'month': MONTH_DICT[month],
                                                          'day': day }))


def redirect_archive_month(request, year, month, object_type):
    return HttpResponsePermanentRedirect(reverse('coltrane_%s_archive_month' % object_type,
                                                 kwargs={ 'year': year,
                                                          'month': MONTH_DICT[month] }))

More like this

  1. @url decorator improvements by davepeck 4 years, 8 months ago
  2. HTTPS redirections middleware with updated URL template tag by xlq 1 year, 6 months ago
  3. FieldsetForm by Ciantic 7 years ago
  4. Sorl Thumbnail + Amazon S3 by skoczen 4 years, 10 months ago
  5. Mobilize your Django site by stevena0 5 years ago

Comments

mbi (on August 25, 2007):

I'd probably use HttpResponsePermanentRedirect instead of HttpResponseRedirect, if the new URLs are going to stick!

#

ubernostrum (on August 25, 2007):

Good catch.

#

(Forgotten your password?)