djangohttp-redirectdjango-viewshttpresponse

django: what's the difference between a temporary redirect and a permanent redirect?


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()?


Solution

  • 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