Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HTML
<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"

value="{{one_listing.listings_id}}">

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

<div class="mb-3">

<label for="imgurl"> Image Upload:

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

</label>

</div>

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

</form>

<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}}"

style="width:10%;">

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

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

{% endif %}

{% endfor %}

</div>


Python
@app.route("/admin/update_listing/<int:id>")

def admin_update_listing(id):

data= {

"id":id

}

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():

Listing.update_one_listing(request.form)

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']

}

Listing_Image.create_listing_images(data)

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

@app.route("/admin/delete_image/<string:photo_name>")

def admin_delete_one_image(photo_name):

data ={

'photo_name' : photo_name

}

Listing_Image.delete_one_listing_image(data)

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.
Posted
Updated 15-Apr-23 10:06am
v2

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
 
Comments
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
 
v2

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