How to use Form in Listview Django?

Asked

Viewed 429 times

8

I am trying to create an action for a Listview, the action would be Delete or change a parameter in the table, for example disable user, using checkbox. If I select all and click on this button the same will delete all selected.

py views.

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')

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>
  • Welcome to [en.so], you do not need to write your posts in two languages, here only fit questions/answers in Portuguese.

  • What is the specific problem with the code? Error is returning?

3 answers

1

I took the example taken from the website How to use class-based views using form:

from myapp.forms import ContactForm
from django.views.generic.edit import FormView

class ContactView(FormView):
    template_name = 'contact.html'
    form_class = ContactForm
    success_url = '/thanks/'

def form_valid(self, form):
    # This method is called when valid form data has been POSTed.
    # It should return an HttpResponse.
    form.send_email()
    return super().form_valid(form)

To see the full documentation follow the link: https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-editing/

1

0

The ideal is you use listview to list yourself. For the action of deleting believe that Voce must create a Deleteview and fire it when deleting your objects. This way your code will be more consistent, easy to understand and within the CBV usage patterns.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.