如何使用Django从API转到特定的外部URL?

问题描述 投票:0回答:1

我是Django的新手,我正在学习如何从我的API转到外部URL,所以我创建了一个项目,在那个项目中,我创建了一个类似'linker'的应用程序,在app的视图中我编写了这段代码:

  # -*- coding: utf-8 -*-                                                     
  from __future__ import unicode_literals                                     
  import urllib3                                                              
  from django.shortcuts import render                                         
  from django.http import HttpResponse                                        
  # Create your views here.                                                   

  def index(request):                                                         
     http = urllib3.PoolManager()                                            
     r = http.request('GET', 'www.google.com')                               
     return HttpResponse(r)

现在这是views.py,代码是对的吗?

python django api
1个回答
0
投票

您不需要urllib3.PoolManager()来进行重定向。使用重定向Django快捷方式:

from django.shortcuts import redirect

def index(request):
    return redirect('https://duckduckgo.com/')
© www.soinside.com 2019 - 2024. All rights reserved.