title: DeleteView with confirmation template and POST method
body: <p>I thought I am doing a standard operation when trying to delete several objects of a model at once with a DeleteView and the POST method, having a confirmation template in between.</p>
  
  <p>Well, either I am sitting too long in front of the computer or this supposedly standard way is not directly supported by Django.</p>
  
  <p>As <a href="https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-editing/#deleteview" rel="nofollow">Djangos docs</a> and other posts say, a confirmation template is only shown (with a Confirmation button) if I use a GET method. But in my case I have a ListView with all my objects of my model and the first column being the checkboxes whose values correspond to the respective objects' IDs.
  I can check several checkboxes at once and then click on my "Delete selected items". The form uses the POST method (I don't want to use GET).</p>
  
  <p>Now: Getting a DeleteView to work with a confirmation template in case of a POST method seems to cause a little of a hack. I did not succeed after several hours.</p>
  
  <p>What is the best way here?</p>
  
  <p>The header of my class looks like this:</p>
  
  <pre><code>class SomeItemConfirmDeleteView(DeleteView):
      template_name = 'confirm_delete_someitems.html'
      model = SomeItem
      success_url = reverse_lazy('list_someitems_url')
      items_to_delete = []
  </code></pre>
  
  <p>Steps done so far:</p>
  
  <ol>
  <li><p>I overwrote the POST method so that if I have a list with all IDs to be deleted coming from the checked checkboxes, I call the GET method so that the confirmation template will be shown. Otherwise, the "Confirm deletion" button has been pressed in the confirm_delete_someitems.html.</p>
  
  <pre><code>def post(self, request, *args, **kwargs):
      self.items_to_delete = self.request.POST.getlist('itemsToDelete')
      if not self.items_to_delete:
          return self.delete(request, *args, **kwargs)
      else:
          return self.get(self, *args, **kwargs)
  </code></pre></li>
  <li><p>I overwrote the get_object method</p>
  
  <pre><code>def get_object(self, queryset=None):
      return self.get_queryset()
  </code></pre></li>
  <li><p>I overwrote the get_queryset method so that the objects to be deleted are displayed in a list in the confirm_delete_someitems.html. I get the objects from the checked checkboxes which are named "itemsToDelete".</p>
  
  <pre><code>def get_queryset(self):
      if not self.items_to_delete:
          queryset = super(ChargeParkConfirmDeleteView, self).get_queryset()
          self.queryset = # ... HOW TO GET THE OBJECTS TO DELETE??
          return self.queryset
      else:
          queryset = super(SomeItemConfirmDeleteView, self).get_queryset()
          self.queryset = queryset.filter(id__in=self.items_to_delete)
          return self.queryset
  </code></pre></li>
  </ol>
  
  <p>The last step is where it gets tricky: how do I get the objects which are to be deleted from the form of confirm_delete_someitems.html:</p>
  
  <pre><code>&lt;form action="" method="post"&gt;
          {% csrf_token %}
          {% trans 'The following objects as well as their related objects will be deleted. Are you sure?' %}
          &lt;ul&gt;
              {% for item in object %}
                  &lt;li&gt;{{ item }}&lt;/li&gt;
              {% endfor %}
          &lt;/ul&gt;
          &lt;input type="submit" value="{% trans 'Confirm deletion' %}" /&gt;
      &lt;/form&gt;
  </code></pre>