Colton Allen
Colton Allen

Reputation: 3060

Getting 400 Error with Flask

This is based off of the Flask tutorial from their official documentation. I'm trying to add a delete option so that I can remove posts I don't want anymore. My if statement is raising the error but I don't know why.

Besides solving my button issue I have a couple of other questions.

Why set the form's action to "."

Why can't I just set the name of the button to delete and ignore the value? (i.e. if request.form['delete']: )

If I wanted to specify which post I was deleting, what would be the best way to get the post-id to this delete page (my testing database has an id column)?

My template:

{% for message in get_flashed_messages() %}
    <div class=flash>
        {{ message }}
    </div>
{% endfor %}

<form action="." method="POST">
    <input type="submit" name="button" value="Delete">
    <a href="{{ url_for('show_entries') }}">Cancel</a>
</form>

My .py file:

@app.route('/delete', methods=['GET', 'POST'])
def remove_entry():
    if request.form['button'] == 'Delete':
        flash("Hello world")

        # delete post
        #g.db.execute('DELETE FROM entries (id) VALUES (?)',something])
        #g.db.commit()
        #return redirect(url_for('show_entries'))

    return render_template('delete.html')

Upvotes: 1

Views: 692

Answers (1)

synthomat
synthomat

Reputation: 774

Why can't I just set the name of the button to delete and ignore the value? (i.e. if request.form['delete']: )

Actually you need to distinguish between a GET- and a POST-Request, like so:

if request.method == 'POST':
    flash("Hello world")

Upvotes: 1

Related Questions