tags:

views:

20

answers:

1

I have updated my url pattern from:

(r'^(?P<slug>[-\w]+)/$', 'bugs.views.bug_detail'),

to

(r'^issue/(?P<id>[0-9]+)/(?P<slug>[-\w]+)/$', 'bugs.views.bug_detail'),

So I'm now relying on the primary key in the URL since the slug can change at any time. I have about 40-50 links that I need to 301 to spiders/crawlers.

What's the easiest way of doing a 301 within Django instead of having to hardcode the Redirect 301s in my Apache conf?

A: 

Ah, got it figured out. I kept the old url pattern in there and routed to bugs.views.bug_detail_redirect and defined a method that uses the slug and does a 301:

from django.http import HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect

def bug_detail_redirect(request,  slug):
    #bug = get_object_or_404(Bug,id=id)
     bug = get_object_or_404(Bug,slug=slug)
     return HttpResponsePermanentRedirect(bug.get_absolute_url())
meder