首页 > 解决方案 > 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],为什么?

标签: pythonpython-requests

解决方案


.json(…)[readthedocs]是一个方法,您应该调用该方法以将数据读取为 JSON Blob:

infos = response.json()  # ←  call the method

推荐阅读