【发布时间】:2020-08-09 23:28:16
【问题描述】:
下午好,我有一个问题,我不知道该怎么做。我正在使用第三方 api 获取数据,我将其存储到 metar 变量中并将其作为参数传递。但是它不起作用。任何帮助将不胜感激。谢谢。
Views.py
from urllib.request import Request, urlopen
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import TemplateView
from django.template.response import TemplateResponse
# Create your views here.
class DashboardView(TemplateView):
template_name = "index.html"
def index(request, template_name="index.html"):
headers = {
'Authorization': 'my_private_api'
}
args={}
request = Request('https://avwx.rest/api/metar/KJFK', headers=headers)
response_body = urlopen(request).read()
args['metar'] = response_body
return TemplateResponse(request,template_name,args)
index.html
{%block content %}
<div>
<h1>Metary</h1>
<p>{{ metar }}</p>
</div>
{%endblock content%}
urls.py
from django.urls import path
from . import views
from dashboard.views import DashboardView
urlpatterns = [
path('', DashboardView.as_view()),
]
【问题讨论】:
标签: python django django-rest-framework django-views django-templates