[FIXED] How to create a pop-up confirmation message button href in Flask Jinja

Issue

I have a Flask application and would like to create a button that displays a popup confirmation message before performing an action. Currently the button is created with an anchor tag and a URL to call her Flask function with the HTTP DELETE method. However, I would like to refactor the code to create popup confirmation messages using only Flask functions and JavaScript.

The current implementation calls his Flask function with the HTTP DELETE method using the anchor tag to delete the product with the given id. The code for the anchor tag looks like this:

Remove

I want to create a confirmation window using JavaScript to create a popup confirmation message before deleting a product. I have implemented JavaScript code to create a confirmation window, but I don’t know how to integrate it with a Flask function to perform a delete action.

The JavaScript code for creating the confirmation window is:



I would like to refactor the code to use only Flask functions and create a popup confirmation message before calling the delete function. Can anyone help me integrate JavaScript code with Flask functions to create the desired behavior?

Solution

The solution

<a href="{{ url_for('productos.delete', id=producto.codprod) }}" onclick="return confirm('Are you sure?')">Delete</a>

and the button style

option 1

<a
  href="{{ url_for('productos.delete', id=producto.codprod) }}"
  onclick="return confirm('Are you sure?')"
  type="button"
  style="text-decoration: none;
    min-width: 100px; 
    max-height: 30px; 
    background-color: #224a64; 
    color: #fff;
    text-align: center; 
    border-radius: 6px; 
    cursor: pointer; 
    right: 0px;
  "
  >Eliminar
  </a>

option 2

<a href="{{ url_for('productos.delete', id=producto.codprod) }}" onclick="return confirm('Are you sure?')"><input type="button" value="Eliminar"></a>

Answered By – devdariill

Answer Checked By – Pedro (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published