繁体   English   中英

Flask render_template 未返回正确的文件

[英]Flask render_template not returning the correct file

我一直在编写 flask 应用程序,该应用程序在本地运行时运行良好,但在托管应用程序时会出现一个奇怪的错误。 在我向服务器发布了一个字符串后,它会以预期的页面进行响应,但是当我用另一个请求(不重新启动服务器)重复它时,再次显示与之前相同的页面。

即使再次运行该命令,在它已经呈现模板之后,render_template 似乎也没有更新我在 HTML 文件中所做的更改。

@app.route("/", methods=['POST', 'GET'])
def index():
    if request.method == 'POST':
        
        content = str(request.form['content'])

        # Some code that changes the "output.html" page based on the user input

        return render_template('output.html')
  
    else:
        return render_template("index.html")


为什么不在if条件中使用 redirect 而不是 render_template ,如下所示:

@app.route("/", methods=['POST', 'GET'])
def index():
    if request.method == 'POST':
        content = str(request.form['content'])
        return redirect(url_for('output.html'))
    else:
        return render_template("index.html")

我找到了解决方案。 显然,如果页面具有与以前相同的 URL,即使在相同的 URL 下呈现新模板,该页面也不会更新。 解决方案是制作动态 URL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM