为什么 Flask 不渲染模板?

时间:2021-01-08 17:50:12

标签: python html image flask file-upload

我想渲染模板,但flask 没有渲染它。由于我必须将图像的路径传递给 HTML 文件,它会在终端上显示路径,但是当我想通过渲染来发送路径时,它不会发送路径。图片来源变得未知。

if __name__ == "__main__":

    @app.route('/', methods=['GET'])
    def index():
        return render_template('index.html')
        
    @app.route('/input', methods=['GET','POST'])
    def result():
        if request.method == 'POST':
            file = request.files['file']
            basepath = os.path.dirname(__file__)
            print(file.filename)
            input_p = file.save(os.path.join(basepath,'uploads\input',secure_filename(file.filename)))
            #output_p = file.save(os.path.join(basepath,'uploads\output',secure_filename(file.filename)))
            cli((os.path.join(basepath,'uploads\input',secure_filename(file.filename))),
                                                             (os.path.join(basepath,'uploads\output',secure_filename(file.filename))) )
            userImage=os.path.join(basepath,'uploads\output',secure_filename(file.filename))
            return render_template('index.html',message=userImage)

最后一行没有渲染模板。我不知道我是在 Post 请求还是 return 语句中传递 get 请求(图像路径),因为它给出了 500 Internal Server Error

    <div class="row py-4">
        <div class="col-lg-6 mx-auto">

            <!-- Upload image input-->
            <div class="input-group mb-3 px-2 py-2 rounded-pill bg-white shadow-sm">

                <form id="upload-file" method="post" enctype="multipart/form-data">
<fieldset>
    
                    <label id="upload-label" for="file" class="font-weight-light text-muted">Choose file
                    </label>

                        <input id="upload" type="file" onchange="readURL(this);" class="form-control border-0" name="file">
</fieldset>           
                </form>
            </div>

            <p class="font-italic text-white text-center">The image uploaded will be rendered inside the box below.</p>
            <div class="image-area mt-4">
                <img id="imageResult" src="#" alt="" class="img-fluid rounded shadow-sm mx-auto d-block">
            </div>
            <button type="button" class="btn btn-primary btn-med" id="btn-predict" style="margin-left: 33%; margin-top: 2%">Convert!</button>
            <div class="image-area mt-7.5" style="margin-top: 10%">

                <img src="{{message}}"  class="img-fluid rounded shadow-sm mx-auto d-block">

            </div>
            <a href="/return-files/{{value}}" target="blank"><button class='btn btn-primary'>Download</button></a>
        </div>
    </div>
</div>
</body>

0 个答案:

没有答案