amoeba
amoeba

Reputation: 571

Django HttpResponsePermanentRedirect don't process my view

If I put this in my view:

if slug == 'old-path':
        return HttpResponsePermanentRedirect('new-path')
it skips my slugbased view and returns 404.
How do I easily return 301 and "reprocess" my view so I don't get a 404?

EDIT
@Pydev UAs comment was the correct answer in this case, but I appreciated the detailed answer by John Debs, which gave me the hint to look into named urls, which I didn't know about. Thanks all.

Upvotes: 0

Views: 1443

Answers (1)

John Debs
John Debs

Reputation: 3784

Add from django.core.urlresolvers import reverse to your list of imports and then try this bit of code:

if slug == 'old-path':
    return HttpResponsePermanentRedirect(reverse('new-path'))

The problem you had was that HttpResponsePermanentRedirect() needs a path but you were providing it with a slug.

reverse() will search through your named URLs for the string you provide and return a path, which can then be redirected to properly.

Upvotes: 2

Related Questions