Forums

FileNotFoundError: [Errno 2] No such file or directory: 'templates/images.jpeg'

hello i'm coding a blog script but filenotfound error appears when uploading photo for the article Code:

path="templates"
app.config['UPLOAD_FOLDER'] = path

@app.route('/upload',methods=['POST'])
def upload():
    title = request.form['title']
    context = request.form['text']
    author = request.form['author']
    img=request.files['img']
    path = os.path.join(app.config['UPLOAD_FOLDER'], img.filename)
    img.save(path)

    new=Posts(title=title,context=context,image=path,author=author)
    db.session.add(new)
    db.session.commit()

    return redirect(url_for('blog'))

Error:

img.save(path)
File "/home/coderrpy/.virtualenvs/python/lib/python3.8/site-packages/werkzeug/datastructures.py", line 3066, in save
 dst = open(dst, "wb")
 FileNotFoundError: [Errno2] No such file or directory : 'templates/images.jpeg'

Build an absolute path for your file saves, not a relative one.