慕尼黑8549860
2021-11-02 09:46:40
我開始學(xué)習(xí) Flask,所以我是這方面的菜鳥,但我已經(jīng)沒有想法可以實現(xiàn),這就是我來這里提問的原因。我有 python 腳本,它向 API 發(fā)出 GET 請求,它返回一個二維碼,之后我得到二維碼并將其添加到我的 html 中,一切正常。但是我有這段代碼檢查 API 給我的 JSON 響應(yīng),它有三個響應(yīng):“加載”、“已驗證”和“獲得二維碼”。req = requests.get('this is the link with my API token')json_content = req.content# parsed JSON content, ready to useparsed_json = json.loads(json_content)@app.route("/")def index(): if parsed_json["accountStatus"] == "loading": print(parsed_json["accountStatus"]) print(req.status_code) return render_template("loading.html") if parsed_json["accountStatus"] == "got qr code": print(parsed_json["accountStatus"]) str_parsed_json = yaml.safe_load(parsed_json["qrCode"]) print(req.status_code) return render_template("qrcodePage.html", str_parsed_json=str_parsed_json) if parsed_json["accountStatus"] == "authenticated": print(parsed_json["accountStatus"]) print(req.status_code) return render_template("index.html")if __name__ == "__main__": app.run(host="0.0.0.0", port=8000, debug=True)我得到的回應(yīng)是 200我在互聯(lián)網(wǎng)上搜索了所有其他人是否有與我相同的問題,但我還沒有找到一個有此問題的人。我試過重新啟動以下重新啟動我的本地服務(wù)器在 app.run() 中使調(diào)試成為 True在 PostMan 中檢查服務(wù)器響應(yīng),但它總是向我返回預(yù)期的結(jié)果,但在頁面中看不到更改。它似乎工作的唯一方法是當(dāng)我對我的代碼進行一些更改并重新啟動服務(wù)器時,即我可以刷新頁面并將我重定向到預(yù)期的模板文件。
1 回答

Qyouu
TA貢獻1786條經(jīng)驗 獲得超11個贊
requests.get根據(jù)您的內(nèi)部對象移動您和所有后續(xù)對象index():
@app.route("/")
def index():
req = requests.get('this is the link with my API token')
json_content = req.content
# parsed JSON content, ready to use
parsed_json = json.loads(json_content)
if parsed_json["accountStatus"] == "loading":
print(parsed_json["accountStatus"])
return render_template("loading.html")
... rest of your code
當(dāng)前requests,您的服務(wù)器未在服務(wù)器啟動后刷新。但是,如果您@app.route("/")每次訪問站點的根頁面時都將其移動到您的內(nèi)部,它將執(zhí)行新操作requests.get()以刷新您的數(shù)據(jù)。
添加回答
舉報
0/150
提交
取消