Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

python - Why Flask is not rendering the Template?

I want to render the template but flask is not rendering it . Since I have to pass the path of the image to the HTML file , it is displaying the path on the terminal but when I want to send the path by rendering it , it is not sending the path . Image source becomes unknown.

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,'uploadsinput',secure_filename(file.filename)))
            #output_p = file.save(os.path.join(basepath,'uploadsoutput',secure_filename(file.filename)))
            cli((os.path.join(basepath,'uploadsinput',secure_filename(file.filename))),
                                                             (os.path.join(basepath,'uploadsoutput',secure_filename(file.filename))) )
            userImage=os.path.join(basepath,'uploadsoutput',secure_filename(file.filename))
            return render_template('index.html',message=userImage)

The last line is not rendering the template . I don't know that whether I am passing get request (image path) in Post request or from return statement because it gives 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...