How to remove items from the database using a flash frame?

I am working with a flash framework and trying to delete a record from the database. The code below shows this error: "This method is not allowed for the requested URL."

In 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 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 get the correct postID from html in py?

+4
source share
2 answers

If you intend to use a POST request, the variable will be available under the jar request.form. If you stay with DELETE, I think you need to change your uri. For instance:

@app.route('/delete/<int:postID>', methods=['DELETE'])
+3
source

To use postID, use {{ loop.revindex }}

This is my code, it works !!

In .py:

@app.route('/delete', methods=['POST'])
def delete_entry():
    if not session.get('logged_in'):
        abort(401)
    db = get_db()
    db.execute('delete from entries where id = ?'[request.form['entry_id']])
    db.commit()
    flash('Entry deleted')
    return redirect(url_for('show_entries'))

In HTML:

<form action="{{ url_for('delete_entry') }}" method=post class=delete-entry>
          <input type="hidden" name="entry_id" value="{{ loop.revindex }}">
          <input type="submit" value="Delete" />
</form>
0
source

All Articles