2017-02-19 65 views
0

我正在嘗試爲ListView創建一個動作,該動作將是刪除或更改模型中表中的參數,例如,使用複選框禁用用戶。 如果我選擇了所有它們並單擊此按鈕,它將被全部刪除。如何在ListView Django中使用表單?

** views.py

class AccountsManagementView(LoginRequiredMixin, generic.ListView): 
    """ 
    Class Based View para a manipulação de usuários. 
    """ 
    model = User 
    template_name = 'accounts/accounts_management.html' 
    context_object_name = 'users' 


    def get_queryset(self): 
     return User.objects.all() 

class DeleteUser(DeleteView): 
    model = User 
    success_url = reverse_lazy('accounts:accounts_manager') 

** urls.py

urlpatterns = [ 
    # ListView dos Usuarios 
    url(r'^$', views.accounts_management, name="accounts_manager"), 
    # Deletar Usuario 
    url(r'^(?P<pk>\d+)/deleteuser/$', views.delete_user, name="delete_user"), 
    # Add Usuario 
    url(r'^adicionar_usuario/$', views.accounts_form, name="accounts_form"), 
    # Update Usuario 
    url(r'^editar/(?P<username>[\w_-]+)/$', views.account_edit, name="accounts_edit"), 
    # Visualizar perfil usuario 
    url(r'^visualizar/(?P<username>[\w_-]+)/$', views.account_view, name="accounts_view"), 
] 

模板/ accounts_management.html

<form action="" method="post"> 
<div class="panel panel-default"> 
    <div class="panel-heading"> 
     <div class="panel-title"> 
      <a href="{% url 'accounts:accounts_form' %}"><button type="button" class="btn btn-success mb_sm"> 
       <i class="fa fa-user-plus" aria-hidden="true" aria-hidden="true"></i> Add User 
      </button></a> 
      <button type="button" class="btn btn-danger mb_sm"> 
       <i class="fa fa-user-times" aria-hidden="true" aria-hidden="true"></i> Del User 
      </button> 
     </div> 
    </div> 
    <div class="panel-body"> 
     <div class="table-responsive"> 
       <table class="table table-striped" id="datatables__example"> 
        <thead> 
        <tr> 
         <th scope="col" class="-colaction-checkboxumn"> 
         <span> 
          <input type="checkbox" name="account-users" id="action-toggle" hidden="hidden"/> 
          <label for="action-toggle"></label> 
         </span> 
         </th> 
         <th>Foto</th> 
         <th>Nome</th> 
         <th>Usuário</th> 
         <th>Grupo</th> 
         <th>E-mail</th> 
         <th>Status</th> 
         <th>Ativo</th> 
         <th>Ações</th> 
        </tr> 
        </thead> 
        <tbody> 
        {% for usuario in users %} 
         <tr class="row1"> 
          <td class="action-checkbox"> 
           <input class="action-select" id="_selected_action" name="account-users" type="checkbox" value="{{ usuario.id }}" hidden="hidden"/> 

          </td> 
          <td scope="row"> 
           <div class="sidebar-user__avatar"> 
            <img src="{{ usuario.imagem|thumbnail_url:'user_image' }}"> 
           </div> 
          </td> 
          <td> 
           <a href="{{ usuario.get_absolute_url }}">{{ usuario.name }}</a> 
          </td> 
          <td>{{ usuario.username }}</td> 
          <td>{% if usuario.groups.all.0 %} 
           {{ usuario.groups.all.0 }} 
          {% else %} 
           <span>Não Atribuído</span> 
          {% endif %}</td> 
          <td>{{ usuario.email }}</td> 
          <td> 
           {% if usuario.is_authenticated %} 
            <i class="fa fa-circle-o fa-lg" style="color: #4aa74e;" aria-hidden="true"></i> Online 
           {% else %} 
            <i class="fa fa-circle-o fa-lg" style="color: #ac2925;" aria-hidden="true"></i> Offline 
           {% endif %} 
          </td> 
          <td> 
           {% if usuario.is_active == True %} 
            {% comment %}<img src="{% static 'assets/ico/correct-45-45.png' %}">{% endcomment %} 
            <a href="#" class="col-xs-1"> <i class="fa fa-check fa-lg" style="color: #4aa74e;" title="Desativar Conta"></i></a> 
           {% else %} 
            {% comment %}<img src="{% static 'assets/ico/incorrect-45-45.png' %}">{% endcomment %} 
            <a href="#" class="col-xs-1"> <i class="fa fa-ban fa-lg" style="color: #ac2925;" title="Ativar Conta"></i></a> 
           {% endif %} 
          </td> 
          <td> 
           <span>&nbsp;&nbsp;&nbsp;</span> 

           <a href="{% url 'accounts:accounts_edit' username=usuario.username %}"> <i class="fa fa-pencil fa-lg" style="color: #464960;" title="Editar Conta"></i></a> 

           <span>&nbsp;&nbsp;&nbsp;</span> 

           <a href="{{ usuario.get_absolute_url }}"> <i class="fa fa-eye fa-lg" style="color: #ec971f;" title="Visualizar Conta"></i></a> 
          </td> 
         </tr> 
        {% endfor %} 
        </tbody> 
       </table> 

       </div> 
      </div> 
     </div> 
</form> 

回答

0

你可以做的是使窗體回報您要刪除的對象的pk列表。

然後,你做這樣的事情:

def delete_view(request): 
    if request.method == "post": 
     data = [] # Array of pks 
     User.objects.filter(pk__in=data).delete() 

這應該做什麼你問。

如果這樣做對你沒有幫助,也許谷歌批量刪除Django。似乎是過去出現的功能,但不再支持。

+0

感謝您的幫助H.Boe。我可以這樣做,我可以在ListView方法POST中使用基於類的視圖。 –

0

查看generic.UpdateView,瞭解使用post()方法的CBV示例。它是在那裏你會帶來一個表格,驗證輸入,並處理 - 在你的情況下,看着每個複選框的價值,並相應地處理記錄。

除非您希望使其可重複使用,否則您可能不需要將邏輯分解爲與通用View相同的方法。

相關問題