0

I m working on a webapp project that using Flask framework .in this app the admin is able to store ,edit and delete an image file from the dataset

I used the code bellow to build the edit process

@app.route("/word/edit/<int:id>", methods=["GET", "POST"])
def edit(id):
    pos = Word.query.get_or_404(id)
    if request.method == 'POST':

        meaning = request.form['meaning']
        image = request.files['image']
        db.session.commit()
        return redirect('/adminpanel')
    else:
        return render_template("edit.html", post=pos)

and here is the code of edit.html

<form method="post" action="/word/edit/{{ post.id }}" enctype="multipart/form-data">
    <dl>
        <p>
            <input type="text" name='meaning' value="{{ post.meaning }}"  required>
        </p>
    </dl>
    <dl>
        <p>
            <input type="file" name='image' value="/download/{{post.meaning}}" required>
        </p>
    </dl>

    <p>
        <input type="submit" value="Upload">

    </p>
</form>

when I m redirected to edit.html I find the correct value in the meaning input but the is no file in the image inpute how can I solve this ,please

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
fatima
  • 23
  • 5

1 Answers1

0

You are trying to assign a value to an <input type=file>. You can't do this, because if you could, you could set value="C:/Users/User/Desktop/super-secret.txt" and hide it using CSS, and when the user clicked submit, you'd be able to get something from their computer without them requesting it.

What you can do instead is add an image tag next to the <input>, initialize it with the current saved image, and update it with the preview of the currently selected image.

Danya02
  • 1,006
  • 1
  • 9
  • 24
  • the image is already saved in the dataset using sqlalchemy ,and what I m trying to do is getting back this stored image as a value of the file input. so the admin can edit it – fatima Aug 13 '21 at 07:39
  • In that case you can add an `` whose `src` points to the address where you serve your image for users, and update the `src` with JS like in the linked answer. – Danya02 Aug 13 '21 at 07:42
  • I can't beacause this must happen in the backend not the frontend .and the problem in flask that the imag are stored as bigbinary and I don't know how to get it back as an inpu value as I can do with the text – fatima Aug 13 '21 at 07:49
  • Well, you already have some way to get the image from the database and out to the user. Just use that endpoint again here. (If you don't know how to return the image from the database, [this might help](https://stackoverflow.com/q/11017466/5936187).) – Danya02 Aug 13 '21 at 07:51