Heila Al-Mogren
Heila Al-Mogren

Reputation: 237

form delete method in flask SQLAlchemy not working

I'm trying to let the page show person details with a button in the bottom to delete the person from the db and directs to home home page. The problem is that for some reason the delete method is not working..

@app.route('/venues/<int:venue_id>')
def show_venue(venue_id):
  data = Venue.query.get(venue_id)
  return render_template('pages/show_venue.html', venue=data)


@app.route('/venues/<int:venue_id>', methods=['DELETE'])
def delete_venue(venue_id):

    try:
        Venue.query.filter_by(id=venue_id).delete()
        db.session.commit()

    except Exception as e:
        print(e)
        error = True
        db.session.rollback()
    finally:
        db.session.close()
        if not error:
            flash('Venue was successfully deleted!')
            return render_template('pages/home.html')
        else:
            flash('An error occurred. Venue ' +
                  request.form['name'] + ' could not be deleted.')
        return None

inside the HTML:

...
    <form class="form" method="DELETE" action="/venues/{{venue.id}}">
        <input type="submit" value="Delete Venue">
    </form>
...

The console shows no errors but it literally does nothing.. just returns to the same page.. thanks for help

Upvotes: 3

Views: 1447

Answers (1)

Bruck1701
Bruck1701

Reputation: 329

I believe that for the delete function, you need to pass what you want to delete as parameter of the function.

item_to_be_deleted =  Venue.query.filter_by(id=venue_id)
db.session.delete(item_to_be_deleted)

EDITED: Instead of using the html-form, if you send the query by Postman, for example, can you delete the entry?

According to the other post linked in the comments, Web browsers can not send methods PUT and DELETE. They only send GET and POST. So it might have something to do with that!!

If you change the method being passed by the browser, it might do the trick!

Upvotes: 2

Related Questions