python - 如何从 api 调用获取有关我的 html 页面的信息
问题描述
我正在尝试通过对 coinmarketcap.com 的 api 调用在我的 html 页面上显示信息。我让用户在上一页中放置一个加密货币的名称,发送一个包含该信息的表单,然后在我的视图中使用它来打印到控制台。我将如何在我的 html 页面上使用这些信息来显示它。
主文件
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
def cryptoLookup(coin):
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/info'
parameters = {
'slug': coin
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': '',
}
session = Session()
session.headers.update(headers)
try:
response = session.get(url, params=parameters)
data = json.loads(response.text)
print(data)
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)
主页.html
<h1>home</h1>
<form action="{% url 'coin' %}" method="POST">
{% csrf_token %}
<h1>Which coin would you like to lookup?</h1>
<input id='coin-lookup' name='coin-lookup' type="text">
<input type="submit">
</form>
enter code here
视图.py
def coin(request):
if request.method == 'POST':
coin = request.POST.get('coin-lookup')
cryptoLookup(coin)
return render(request, 'app/coin.html')
解决方案
推荐阅读
- c# - Unity Container 返回单例而不是新实例
- ocaml - 路径中缺少 ocamlbuild
- python-3.x - 输入整数时计算最小硬币数量的程序是正确的,但不是浮点数
- java - 由 DI 序列定义的从 x 开始的排列计数的代码优化
- xamarin.forms - 单击按钮时如何获取itemlistview详细信息
- django - 如何使用django框架制作vue js ssr
- java - 是否可以在 Java 中将方法体作为参数传递?
- dom - 跨域 iFrame 中的凭据填充
- javascript - 带有 % 的 if 语句在 Javascript 代码中不起作用
- symfony4 - 如何定义输入以在 Easy admin 中保存浮点数?