运行 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() 内部计算的,我该如何引用该变量?需要全局变量吗?
1 回答

慕慕森
TA贡献1856条经验 获得超17个赞
您可以从函数返回这些变量。
您必须解压缩从函数发送的变量upload。
首先,您必须将它们从 退回upload,然后将其拆包发送到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
提交
取消