Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect to external URL in Django?

I think this should be easy, but I cannot figure it out. I am trying to write an opt-out view. I am receiving a get request. Through urls.py, I render my opt-out view. In this view, I save some parameters for the user in the database and then I want to redirect the user to an external URL. I tried:

return redirect('http://stackoverflow.com/') 

from Django documentation. However, the optout view renders the training template instead of returning the redirect, though the parameters are saved in the database as expected. My code is as follows:

def optout(request):     if (('REMOTE_USER' in request.META and request.META['REMOTE_USER'] != "") or          (request.session.get('userid', False) and request.session['userid'] != "")):         if ('REMOTE_USER' in request.META and request.META['REMOTE_USER'] != ""):             userid = request.META['REMOTE_USER']         if (request.session.get('userid', False) and request.session['userid'] != ""):             userid = request.session['userid']         user = User.objects.get(username=userid)         user.optout = True         user.postpone = False         user.save()         return redirect('http://stackoverflow.com/')     context = { 'userid': "" }     return render(request, 'games/Training.html', context) 

Any help is highly appreciated.

like image 786
1man Avatar asked Mar 09 '16 22:03

1man


People also ask

How do I reference a URL in Django?

The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py . Listing 2-16 shows how to name a project's home page, as well as how to reference this url from a view method or template.

How do I redirect in Django?

Django Redirects: A Super Simple Example Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .

How do I redirect a URL in Python?

The redirect() function allows us to redirect a user to the URL of our choice. In the Flask application that we are building so far, we have a /shortenurl route that checks to see what method type is in use. If it is a GET request, we are simply returning some text to the user.

How does URL routing work in Django?

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info . Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view).


2 Answers

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.

like image 169
sytech Avatar answered Sep 21 '22 07:09

sytech


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

like image 35
sandes Avatar answered Sep 23 '22 07:09

sandes