Removing rows from database using python flags?

I am using a flash structure and cannot delete rows from the database. The code below gives error 405: "This method is not allowed for the requested URL." Any ideas?

In py:

@app.route('/delete/<postID>', methods=['POST'])
def delete_entry():
    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', post=post))

In html:

<a href="/delete/{{ entry.id }}"><h3>delete</h3></a>
+2
source share
4 answers

By clicking <a href...>delete</a>, you will receive a GET request, and your delete_entry method will respond only with POST.

You need to either: 1. replace the link to the form and submit the button or 2. send the link to the hidden form using JavaScript.

Here's how to do 1:

<form action="/delete/{{ entry.id }}" method="post">
    <input type="submit" value="Delete />
</form>

Here's how to do 2 (using jQuery):

$(document).ready(function() {
    $("a.delete").click(function() {
        var form = $('<form action="/delete/' + this.dataset.id + '" method="post"></form>');
        form.submit();
    });
});

...

<a href="#delete" class="delete" data-id="{{ entry.id }}">Delete</a>

, , - delete_entry GET. GET ( ). .

+3

, POST DELETE, .

@app.route('/delete/<postID>', methods=['DELETE'])

HTTP DELETE.

+2

flaskr Flask ( , ).

.py:

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

HTML:

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

, .

+1

A simple <a href=HTML link sends a request GET, but your route only allows requests PUT.

<a>does not support requests PUT. You must submit a request with a form and / or with JavaScript code. (See Use a POST link instead of a GET .)

0
source

All Articles