django外调用url_如何在Django中重定向到外部URL?

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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值