Flask render_template 不会更新页面。

huangapple go评论61阅读模式
英文:

Flask render_template does not update the page

问题

以下是您要翻译的内容:

"我试图上传一个文件,进行一些计算以生成图像,然后显示它。我想出了以下解决方案,但在“重定向”后页面没有刷新。尽管我得到了正确的代码:

"POST /result HTTP/1.1" 302 -
"GET /display_image/census_2009b.png HTTP/1.1" 200 -

Flask render_template 不会更新页面。

from flask import Flask, request, render_template, send_file, redirect, url_for

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/result', methods=['GET', 'POST'])
def upload_file():
    file = request.files['file']
    ### 生成并保存图像到 `filename`...

    return redirect(url_for('display_image', filename=filename))

@app.route('/display_image/<filename>', methods=['GET', 'POST'])
def display_image(filename):
    image_url = url_for('static', filename=filename)
    return render_template('display_image.html', image_url=image_url)

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

使用 index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <script src="https://unpkg.com/dropzone@5/dist/min/dropzone.min.js"></script>
        <link rel="stylesheet" href="https://unpkg.com/dropzone@5/dist/min/dropzone.min.css" type="text/css" />
    </head>
    <body>
        <form action="/result" method="get" class="dropzone" id="my-dropzone"></form>
    </body>
</html>

以及 display_image.html

<!DOCTYPE html>
<html>
<head>
    <title>上传的图像</title>
</head>
<body>
    <img src="{{ image_url }}" alt="上传的图像">
</body>
</html>

我漏掉的部分是什么?"

英文:

I am trying to upload a file, make some calculations to generate an image and then to display it. I came up with the following, but the page does not refresh after the redirect. Even though I get the correct codes:

&quot;POST /result HTTP/1.1&quot; 302 -
&quot;GET /display_image/census_2009b.png HTTP/1.1&quot; 200 -

Flask render_template 不会更新页面。

from flask import Flask, request, render_template, send_file, redirect, url_for

app = Flask(__name__)

@app.route(&#39;/&#39;)
def index():
    return render_template(&#39;index.html&#39;)

@app.route(&#39;/result&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])
def upload_file():
    file = request.files[&#39;file&#39;]
    ### generating and saving an image to `filename`...

    return redirect(url_for(&#39;display_image&#39;, filename=filename))

@app.route(&#39;/display_image/&lt;filename&gt;&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])
def display_image(filename):
    image_url = url_for(&#39;static&#39;, filename=filename)
    return render_template(&#39;display_image.html&#39;, image_url=image_url)

if __name__ == &#39;__main__&#39;:
    app.run(host=&#39;0.0.0.0&#39;, debug=True)

with index.html:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
    &lt;head&gt;
        &lt;meta charset=&quot;UTF-8&quot;&gt;
        &lt;script src=&quot;https://unpkg.com/dropzone@5/dist/min/dropzone.min.js&quot;&gt;&lt;/script&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;https://unpkg.com/dropzone@5/dist/min/dropzone.min.css&quot; type=&quot;text/css&quot; /&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form action=&quot;/result&quot; method=&quot;get&quot; class=&quot;dropzone&quot; id=&quot;my-dropzone&quot;&gt;&lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;

and display_image.html:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Uploaded Image&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;img src=&quot;{{ image_url }}&quot; alt=&quot;Uploaded image&quot;&gt;
&lt;/body&gt;
&lt;/html&gt;

What am I missing?

答案1

得分: 0

Your Flask functions are fine, the issue is with Dropzone. Dropzone controls the upload process, so you have to use Dropzone to redirect once the upload is complete. Check out this post for details on how to redirect with Dropzone.

I'd also consider implementing Flask Dropzone (made by the guy who answered the question linked above), it has very good documentation.

英文:

Your Flask functions are fine, the issue is with Dropzone. Dropzone controls the upload process, so you have to use Dropzone to redirect once the upload is complete. Check out this post for details on how to redirect with Dropzone.

I'd also consider implementing Flask Dropzone (made by the guy who answered the question linked above), it has very good documentation.

huangapple
  • 本文由 发表于 2023年5月6日 21:53:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定