Click here to Skip to main content
15,794,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
<form action="/listing_image_upload" method="post" class="contact-form" enctype="multipart/form-data"

style="padding: 30px; margin-left: 30px; margin-right:30px;">

<input type="hidden" class="form-control" name="listings_id" id="listings_id"


<input type="hidden" class="form-control" name="listing_id" id="listing_id" value="{{}}">

<div class="mb-3">

<label for="imgurl"> Image Upload:

<input type="file" class="form-control" name="photo_name" id="photo_name">



<button type="submit" class="btn btn-primary">UPLOAD IMAGES</button>


<div style ="display: flex;">

{% for image in all_images %}

{% if one_listing.listings_id == image.listings_id %}

<img class="myImg" src="{{ url_for('static', filename = 'img/listing_images/') }}{{image.photo_name}}"


<a href="/admin/delete_image/{{image.photo_name}}"

style="text-decoration:none; color: red;">DELETE IMAGE</a>

{% endif %}

{% endfor %}



def admin_update_listing(id):

data= {



return render_template("admin/listings/admin_update_listings.html", one_listing = Listing.get_one_listing(data),

all_agents = Agent.get_all(), all_property_types = Property.get_all_property_types(),

all_offer_types = Offer.get_all_offer_types(), all_states = State.get_all_states(),

all_cities = City.get_all_cities(), all_images = Listing_Image.get_all_listing_images())

@app.route("/listing_update", methods = ["POST"])

def admin_listing_update():


return redirect(f"/admin/update_listing/{request.form['id']}")

@app.route("/listing_image_upload", methods = ["POST"])

def admin_listing_image_upload():

data ={

'id' : id,

'photo_name': upload_image('listing'),

'listings_id': request.form['listings_id']



return redirect(f"/admin/update_listing/{request.form['listing_id']}")


def admin_delete_one_image(photo_name):

data ={

'photo_name' : photo_name



os.remove(os.path.join('static/img/listing_images/', photo_name))

return redirect('/admin/listings') #need this to redirect to "/admin/update_listing/<int:id>"

What I have tried:

I tried to use the hidden input as it was used in the upload image route.
Updated 15-Apr-23 11:06am

You're making a GET request to the delete_image route, using an <a> tag, so you won't be able to include hidden fields in the request.

But endpoints which create, change, or delete data should never accept GET requests. Some browsers or proxy-servers may try to pre-load the URL to make navigation faster; if that happens, all of your images will be deleted as soon as the page is loaded.

Instead, make the delete_image endpoint require a POST request, and use another <form> to call it.
Share this answer
vishanish 15-Apr-23 16:04pm    
Thank You For Your Help
This issue has been resolved. Thank you for your help. I changed my original code to call and used the POST request for the delete_image endpoint.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900