Home>

I want to use Django to select multiple items displayed in ListView with checkboxes, then press the delete button to display the modal, and then press the modal delete button before deleting. ..

Currently, if you press the delete button after selecting multiple items, a modal will appear and even if you press delete again, it will not be deleted from the ListView. I presume that the information selected multiple times on the screen displayed in ListView is not submitted by the delete button in the modal. By the way, without modal, it was deleted normally. I searched a lot but couldn't find a solution. I would appreciate it if you could teach me.

Corresponding source code
<form method = "post" action = "{% url'commons: delete'%}">
    {% csrf_token%}
<table>
<thead>
  <tr>
      <th scope = "col"><input type = "checkbox" name = "allChecked">Select all displayed items</th>
      <th scope = "col">Item ⇅</th>
      <th scope = "col">Updated date and time ⇅</th>
  </tr>
</header>
<tbody>
  {% for word in object_list%}
  <tr>
        
          <td scope = "row"><input type = "checkbox" name = "search_word_list" value = "{{word.negative_word}}"></td>
        
    <td>{{word.negative_word}}</td>
    <td>{{word.update_time}}</td>
  </tr>
  {% endfor%}
</tbody>
</table>
<br />

     
        
            {% include'snippets/pagination.html'%}<br />
          <button type = "button" data-toggle = "modal" data-target = "# del_dialog_Modal">Delete selected item</button>
        
     

</form>
<!-Delete dialog->



    
        Delete selected item
    
    
      <p>Deletes the selected item. Once deleted, it cannot be undone.</p>
    
    
      <button type = "button" data-dismiss = "modal">Back</button>
         Delete selected item 
    


  • Answer # 1

    By the way, without modal, it was deleted normally.

    I mean

    <button type = "button" id = "delete" data-toggle = "modal" data-target = "# del_dialog_Modal">Delete selected item</button>

    Does it mean that it worked normally if this was submitted?

    In this modal content, it is a GET method that just jumps to the link, so it does not mean that you are sending a form.

    Delete selected item

    You can submit from outside the form by using the following usage
    Example 1
    Use only html
    Add form attribute to button and describe form id

    <form method = "post" id = "myform" action = "{% url'commons: delete'%}">
    <!-Omitted->
    </form>
    <button type = "submit" form = "myform">submit</button>

    Example 2
    Use JavaScript
    Add name attribute to formvalue of document.name attribute .submit ()Can be submitted with

    <form method = "post" name = "myform" action = "{% url'commons: delete'%}">
    <!-Omitted->
    </form>
    <button onclick = "document.myform.submit ()" type = "button">Submit</button>

Related articles