【发布时间】:2020-12-15 22:55:56
【问题描述】:
我正在创建一个网页,我想使用 Flask 的 render_template 转到该页面的特定部分(在本例中称为结果)。
有没有办法做到这一点,例如通过将参数 go_to_results = True 传递给 render_template:
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return render_template('index.html')
file = request.files['file']
if file.filename == '':
return render_template('index.html')
prediction = predict(file, 3, percentage=True)
return render_template('index.html', prediction_html = prediction, go_to_results = True)
return render_template('index.html')
我找到了这个示例,但它不符合我的需要,因为我只使用了 1 个 HTML 页面('index.html')。 Link to a specific location in a Flask template.
更新工作解决方案: 我将此添加到 HTML 结果部分:
<a href="#results">
</a>
然后我使用了这个 JS 代码。
<script>
function jumpToHash(hash){
if (hash){
var url = location.href;
location.href = "#results";
}
}
jumpToHash({{ anchor }});
</script>
【问题讨论】: