python - Django“方法”对象不可下标
问题描述
我对 django 有疑问,我从 Html 索引页面获取名称,然后我想请求数据。在我想用 gettin html 名称访问该数据之后。
from flask import Flask,render_template,request
import requests
api_key = "35c1fbfe2a35fd3aaf075e7394bf8855"
url = "http://data.fixer.io/api/latest?access_key="+api_key
app = Flask(__name__)
@app.route("/" , methods = ["GET","POST"])
def index():
if request.method == "POST":
firstCurrency = request.form.get("firstCurrency") # EURO
secondCurrency = request.form.get("secondCurrency") # TRY
amount = request.form.get("amount") #20
response = requests.get(url)
#app.logger.info(response)
infos = response.json
firstValue = infos["rates"][firstCurrency]
secondValue = infos["rates"][secondCurrency]
result = (secondValue / firstValue) * float(amount)
currencyInfo = dict()
currencyInfo["firstCurrency"] = firstCurrency
currencyInfo["secondCurrency"] = secondCurrency
currencyInfo["amount"] = amount
currencyInfo["result"] = result
#app.logger.info(infos)
return render_template("index.html",info = currencyInfo)
else:
return render_template("index.html")
if __name__ == "__main__":
app.run(debug = True)
我这条线有问题firstValue = infos["rates"][firstCurrency]
,为什么?
解决方案
.json(…)
[readthedocs]是一个方法,您应该调用该方法以将数据读取为 JSON Blob:
infos = response.json() # ← call the method
推荐阅读
- jquery - jQuery Ajax post 无法发送数据
- c# - 将 WPF Datagrid HorizontalScrollBar 锁定到窗口底部
- reactjs - 在创建反应应用程序之前是否必须创建一个文件夹
- ruby-on-rails - 如何在 activeadmin 仪表板中查看所有用户发出的请求?
- android - Google Cloud Storage 存储分区 ID
- node.js - 在编译时将基于包名称的变量传递给 webpack 源
- php - 我在数据表的每一列中都有复选框我想在选中复选框时停止排序
- java - 使用java interthread在控制台中传输文件
- node.js - 是否可以在一个 javascript 文件中添加来自 Node.JS 的库和来自 angular.js 以及 react.js 的一些库?
- python - 检查字符串是否是 Tkinter Text Widget 中的单词或单词的一部分