31

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.

1man
  • 5,216
  • 7
  • 42
  • 56
  • 1
    What do you mean by _it does not work_? The optout view renders the Training template instead of returning the redirect? – John Gordon Mar 09 '16 at 22:22
  • @JohnGordon, yes. You're right. The optout view renders the Training template instead of returning the redirect, though the parameters are saved in the database as expected. – 1man Mar 09 '16 at 22:26
  • similiar question is answered [here](http://stackoverflow.com/questions/12326504/django-redirect-to-custom-url) – AramirezMiori Mar 09 '16 at 22:30
  • 4
    If it's rendering the Training template, then very likely your top `if` statement is evaluating to false (and thus redirect is never called). The database save must be happening elsewhere. Try putting in some logging statements. – John Gordon Mar 09 '16 at 22:30
  • Did you import redirect? from django.shortcuts import redirect – moojen Jul 22 '19 at 14:43

2 Answers2

35

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.

Winston
  • 601
  • 1
  • 9
  • 29
sytech
  • 29,298
  • 3
  • 45
  • 86
  • Yes Gator_Python. Thank you for your response. I checked the database for a number of times and both parameters are saved correctly. – 1man Mar 09 '16 at 22:23
  • So, what's happening instead of the expected redirect? – sytech Mar 09 '16 at 22:27
  • The optout view renders the Training template instead of returning the redirect, though the parameters are saved in the database as expected. – 1man Mar 09 '16 at 22:27
  • 1
    Instead of checking your database, try printing out the conditionals you're checking for to see if they match what you expect. Or change that conditional to `if True` just for the sake of proving the `return redirect` portion is working. Then we can narrow our scope to figure out why those aren't catching, which is what I suspect is happening. – sytech Mar 09 '16 at 22:29
  • Is there a way we could open the link on a new tab, as we do it in HTML using `_blank`? – Anurag-Sharma Jun 25 '20 at 00:11
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

sandes
  • 1,917
  • 17
  • 28