0

In my flask app I want the user to be able to upload an image to a folder inside the static folder - called wallpaper. Currently I'm receiving a flask.debughelpers.DebugFilesKeyError, however I don't see any error in the keys I'm using


What I've tried


flaskapp.py

@app.route('/changeWallpaper' , methods = ['POST', 'GET'])
def change_home_wallpaper():

    UPLOADS_PATH = join(dirname(realpath(__file__)), 'static\\wallpaper')

    if request.method == "POST":
        wallpaper = request.files['wallpaper']
        if wallpaper.filename != '':
                image = request.files['wallpaper']
                image.save(os.path.join(UPLOADS_PATH, secure_filename(image.filename)))

        cur = db2.cursor()
        sql = f"UPDATE wallpaper set pic = {wallpaper.filename} where sno = 1"
        cur.execute(sql)
        db2.commit()
        cur.close()
        return redirect(url_for('home'))
    else:
        return redirect(url_for('home'))

loggedin.html

<div class="jumbotron">
    <form action="{{url_for('change_home_wallpaper')}}" method="post">
        <div class="container">
            <input type="file" name="wallpaper"/>
            <input type="submit" class="btn btn-primary"/>
        </div>
    </form>

</div>
2
  • Check it out here stackoverflow.com/a/54559035/8843270, this might give you some clues Commented Oct 5, 2020 at 13:27
  • 1
    @simkus Thanks I had to include enctype="multipart/form-data", and put {wallpaper.filename} in quotes. Do you mind adding it as an answer Commented Oct 5, 2020 at 13:36

1 Answer 1

1

Update your HTML

<form action="/path" method="post" enctype="multipart/form-data">
</form>

and put {wallpaper.filename} in quotes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.