Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete items from database using a Flask framework?

I'm working with a Flask framework, and am trying to delete an entry from the database. The code below gives this error: "The method is not allowed for the requested URL."

In the html:

<form action="{{ url_for('delete_entry', id=entry.id) }}" method="POST">
     <input type="hidden" name="_method" value="DELETE" />
     <input type="submit" value="Delete entry" />
</form>

In the py:

@app.route('/delete', methods=['DELETE'])
def delete_entry(postID):
    if not session.get('logged_in'):
        abort(401)
    g.db.execute('delete from entries WHERE id = ?', [postID])
    flash('Entry was deleted')
    return redirect(url_for('show_entries'))

How do I then get the correct postID from the html to the py?

like image 815
Hayley van Waas Avatar asked Sep 19 '14 01:09

Hayley van Waas


People also ask

How do I delete a record in SQLAlchemy?

We can go for deletion of all the rows existing inside a particular table by using the delete() method of SQLAlchemy.

How do you delete a method in Flask?

route("/guide/<id>", methods=["DELETE"]) def guide_delete(id): Take in an ID as an argument, and then we're going to first perform the query. From there we're going to start up a session. Make sure you execute it with a parens, and then from there we are simply going to return guide_schema.


1 Answers

If you are going to use a POST request the variable will be available under flask's request.form. If you stay with DELETE I think you need to change your uri. For example:

@app.route('/delete/<int:postID>', methods=['DELETE'])
like image 195
Wilberto Avatar answered Oct 31 '22 20:10

Wilberto