[Django]-How to redirect to external URL in Django?

36👍

Yeah, return redirect('http://stackoverflow.com/') is the correct method.

If you do the following, you can confirm that is a working method to redirect.

   from django.shortcuts import redirect

   def optout(request):
       return redirect("http://stackoverflow.com/")

Your conditional statements must not be catching.

18👍

using class HttpResponseRedirect

from django.http import HttpResponseRedirect

def out(request):
    return HttpResponseRedirect("http://google.com")

Or:

using class HttpResponse

from django.http import HttpResponse

def out(request):
    response = HttpResponse("", status=302)
    response['Location'] = "scheme://host"
    return response

NOTE:

The last one is useful to redirect from a website to a mobile
(Android/Iphone) app. Where location is scheme://host

Leave a comment