How to use Form in ListView Django?

5

I'm trying to create an action for a ListView, the action would be to delete or change a parameter in the table, for example, disable user, using checkbox. If I select all and click this button it will delete all selected.

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"),
]

templates / 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>
    
asked by anonymous 19.02.2017 / 14:46

1 answer

0

The ideal is to use the listview only to list. For the deletion action I believe you should create a DeleteView and fire it when deleting your objects. This way your code will be more consistent, easy to understand and within the patterns of use of the CBV.

    
25.10.2017 / 12:21