Question

Okay so if you go to the documentations here: https://docs.djangoproject.com/en/dev/topics/http/shortcuts/

and scroll down to look at the last example of redirect() it says "By default, redirect() returns a temporary redirect. All of the above forms accept a permanent argument; if set to True a permanent redirect will be returned:"

Now, what's the difference between a temporary redirect and a permanent redirect? I'm using it so that, when a user logs in and is authenticated, then to redirect him to the logged in page. Should I be using HttpResponseRedirect() instead? Does it give any benefit of using redirect() instead of HttpResponseRedirect()?

Was it helpful?

Solution 2

There are two ways to return a 301 permanent redirect:

from django.shortcuts import redirect

def my_view(request):
    # some code here
    return redirect('/some/url/', permanent=True)

https://docs.djangoproject.com/en/1.5/topics/http/shortcuts/#redirect

or:

from django.http import HttpResponsePermanentRedirect

    def my_view(request):
        # some code here
        return HttpResponsePermanentRedirect('/some/url')

https://docs.djangoproject.com/en/1.5/ref/request-response/#django.http.HttpResponsePermanentRedirect

OTHER TIPS

Just adding a note to Brandon's post concerning your question what is the difference between both. The major difference between temporary and permanent redirects is how third parties see it. If Google sees a permanent redirect it would probably think that the old site is gone and would update all hits to directly access the new URL. Some people say that Google favors permanent redirects as temporary redirects are often used by spammers.

A case for temporary redirects are e.g. internal rewritings of URLs on your own site that link to a download mirror/static file server. In this case the redirecting site will probably stay the same while the other server address might change (e.g. host static content wherever it is cheapest). Using permanent redirects might introduce problems (think about podcast players on your phone that remember such redirects and won't recognize it when you move your static file server).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top