Contenido del Curso
Flask Intensive Course: Web Development with Python
Flask Intensive Course: Web Development with Python
Implement Delete Functionality
Time to add the DELETE method to our application.
We'll begin by implementing the endpoint first and then move on to the template. This sequence makes the most sense.
Let's define a new route for delete
and specify which post we intend to remove.
We use the get_or_404()
method because we don't want to break if the object does not exist.
And after deleting, we need to redirect back to the recipes page.
Next step, we need to have the Delete button on our website. Go to recipes.html. After the description, just put a simple link.
We use the get_or_404()
method to prevent any issues in case the object doesn't exist. After deletion, we must redirect back to the /recipes page.
The next step is to add the Delete button to our website. To do this, navigate to recipes.html. Following the description, simply insert a straightforward link after the paragraph with the description.
It's just getting the id
of the recipe, then it's fetching that, and it's going to the URL.
Now, refresh it. As you can see, the button becomes active.
If we are going to delete the last recipe, it will be gone.
¡Gracias por tus comentarios!