The code to show the table, and also with the button to delete a row.
The code to delete the row from the table
I am trying to create a button that delete a row from a table, but I don´t know how to fix this. Sorry for the images, I couldn´t insert code.
I think the problem is with the id, maybe with calling the function also.
The id attribute is an identifier for the html tag and is not sent. Furthermore, a button alone cannot send data. An anchor or an entire form must be used for this. Which variant you use depends on the type and purpose of the transfer.
If you want to use a GET request, an anchor is sufficient. The data is transmitted as part of the URL. Either embedded or as optional parameters. This corresponds to the standard type of request that a browser sends to call up a page, for example via the address bar, and is easy to do.
<a href="{{ url_for('remove', id=entry.id) }}">Remove</a>
If you need a POST request, use a form. Here the data is sent in the request body and is more protected from the view of third parties and less restricted in size. This request cannot be realized via the address bar.
A form is also necessary if the user is to make entries, for example for a search query, and a GET request is still used.
<form method="POST" action="{{ url_for('remove', id=entry.id) }}">
<button type="submit">Remove</button>
</form>
Depending on the type of request, the data can be requested on the server in different ways. The examples above use variable rules to send data from client to server.
Here's a simple endpoint for a GET request. For multiple or other request types, these need to be specified using the methods
attribute.
@app.route('/delete/<int:id>')
def remove(id):
# ...
Optional parameters of a GET request can be requested via request.args
, form data of a POST request via request.form
. In addition, there are separate options for files and JSON data. The documentation explains this for the respective purpose.