如何使用 'q' 调用 API 并在 Django 视图中获得响应?
How do I use a 'q' to call an API and get a response in Django views?
我正在尝试查询此 api 并获得响应,但我显然没有正确执行此操作,因为我知道它没有这么简单。
from django.shortcuts import render
def home(request):
response = request.GET["https://api-adresse.data.gouv.fr/search/?q=8+bd+du+port"}
return render(request, "home.html", {'response': response})
我希望能够提供 API 的地址,即:“8 Boulevard du Port 80000 Amiens”和
获取有关它的相关信息。即:gps坐标
这是 api: https://adresse.data.gouv.fr/api
我似乎找不到有关如何使用 Django 中的视图发出此类请求和处理响应的信息。
request.GET
是一个类似字典的对象,包含所有给定的 HTTP GET 参数,也称为查询参数或查询字符串。
requests
是 Python 的简单 HTTP 库,允许您发送 HTTP/1.1 请求。
from urllib.parse import quote_plus
import requests
from django.shortcuts import render
def home(request):
url = "https://api-adresse.data.gouv.fr/search/?q={}"
address = "8 Boulevard du Port 80000 Amiens"
response = requests.get(url.format(quote_plus(address)))
# response.json() # this will give you JSON response
return render(request, "home.html", {"response": response})
我正在尝试查询此 api 并获得响应,但我显然没有正确执行此操作,因为我知道它没有这么简单。
from django.shortcuts import render
def home(request):
response = request.GET["https://api-adresse.data.gouv.fr/search/?q=8+bd+du+port"}
return render(request, "home.html", {'response': response})
我希望能够提供 API 的地址,即:“8 Boulevard du Port 80000 Amiens”和 获取有关它的相关信息。即:gps坐标
这是 api: https://adresse.data.gouv.fr/api
我似乎找不到有关如何使用 Django 中的视图发出此类请求和处理响应的信息。
request.GET
是一个类似字典的对象,包含所有给定的 HTTP GET 参数,也称为查询参数或查询字符串。
requests
是 Python 的简单 HTTP 库,允许您发送 HTTP/1.1 请求。
from urllib.parse import quote_plus
import requests
from django.shortcuts import render
def home(request):
url = "https://api-adresse.data.gouv.fr/search/?q={}"
address = "8 Boulevard du Port 80000 Amiens"
response = requests.get(url.format(quote_plus(address)))
# response.json() # this will give you JSON response
return render(request, "home.html", {"response": response})