ibeautiful
2023-02-22 15:46:22
運行 routes.py@app.route('/articles', methods=['GET', 'POST'])def articles(): upload() return render_template('articles.html')而這個保存圖像并處理其信息的功能def upload(): if request.method == 'POST': # Save the file to static/uploads label = "some string from process above" probability = "another string" return None return None如何在渲染模板時使用變量標簽和概率?有些人使用接近于:return render_template('articles.html', label=label, probability=probability)這樣做是為了使用 js 引用變量。但是,如果它是在 upload() 內(nèi)部計算的,我該如何引用該變量?需要全局變量嗎?
1 回答

慕慕森
TA貢獻1856條經(jīng)驗 獲得超17個贊
您可以從函數(shù)返回這些變量。
您必須解壓縮從函數(shù)發(fā)送的變量upload。
首先,您必須將它們從 退回upload,然后將其拆包發(fā)送到render_template。
@app.route('/articles', methods=['GET', 'POST'])
def articles():
label, probability = upload()
return render_template('articles.html', label=label, probability=probability)
def upload():
if request.method == 'POST':
# Save the file to static/uploads
label = "some string from process above"
probability = "another string"
return (label, probability)
return (None, None)
添加回答
舉報
0/150
提交
取消