Home > Software engineering >  "GET /static/uploads/IMG_4575_sketch.jpg HTTP/1.1" 404 -
"GET /static/uploads/IMG_4575_sketch.jpg HTTP/1.1" 404 -

Time:01-16

I can't represent picture in rendered html file from static folder.

I tried with this block of code, but it doesn't work for me. Does anyone have other idea?

<div  style="margin-top:10px;margin-bottom:10px;">
    <div >
        <h2>Original Image</h2><img src="{{ url_for('static', filename ='{{ original_name }}') }}"
                style="display: block;margin-left: auto;margin-right: auto;">
    </div>
    <div >
        <h2>Sketch Image</h2><img src="{{ url_for('static', filename = '{{ sketch_img_name }}') }}"
                style="display: block;margin-left: auto;margin-right: auto;">
    </div>
</div>

Pc, here is the method:

UPLOAD_FOLDER = '/home/luka/Test/flask_scatch/static'
@app.route('/sketch',methods=['POST'])
def sketch():
    file = request.files['file']
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        img = cv2.imread(UPLOAD_FOLDER '/' filename)
        sketch_img = make_sketch(img)
        sketch_img_name = filename.split('.')[0] "_sketch.jpg"
        _ = cv2.imwrite(UPLOAD_FOLDER '/' sketch_img_name, sketch_img)
        return render_template('home.html',original_name=filename,sketch_img_name=sketch_img_name)

CodePudding user response:

The problem with your code is that you are using double curly braces inside the url_for function call. This is causing the filename argument to be passed as a string instead of a variable.

Try removing the extra curly braces, so it should look like this:

<img src="{{ url_for('static', filename = original_name) }}" style="display: block;margin-left: auto;margin-right: auto;">
  • Related