I have a Listview class that show a table (working), I also have a django_filter for filtering the table (working).
I want a button in template Listview , that open the django_filter_form ( not working, only show submit buttom), how do that ?
views.py
from django.shortcuts import render
from django.views.generic import ListView
from django.views.generic.edit import FormView
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .models import Cmsexport
from .filters import CmsFilter
from django.contrib.auth.decorators import login_required
class Cms(ListView):
queryset = Cmsexport.objects.all().values()
model = Cmsexport
template_name = 'table.html'
context_object_name = 'cms'
paginate_by = 10
def get_queryset(self):
queryset = super().get_queryset()
self.filterset = CmsFilter(self.request.GET, queryset=queryset)
return self.filterset.qs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = self.filterset.form
return context
filters.py:
from django_filters import FilterSet
from .models import Cmsexport
class CmsFilter(FilterSet):
class Meta:
model = Cmsexport
fields = {
'name': ['icontains'],
'state_text': ['icontains'],
'is_virtual': ['icontains'],
}
urls.py:
from django.urls import path
from django.views.generic import TemplateView
from . import views
from .views import Cms
urlpatterns = [
path('', views.dashboard, name='dashboard'),
path('table/', views.table_view, name='table_view'),
path('cms/', Cms.as_view(), name='cms'),
path('filters_form/', TemplateView.as_view(template_name='filters_form.html'), name='filters_form'),
path('search/', views.search, name='search'),
]
template/table.html:
{% extends 'base.html' %}
{% load crispy_forms_filters %}
{% load crispy_forms_tags %}
{% block content %}
{% include 'filters_form.html' %}
<a href="{% url 'filters_form' %}">Search</a>
<table class="table table-striped">
<thead>
<tr>
{% for item in object_list %}
{% for header in item.keys %}
<th>{{ header }}</th>
{% endfor %}
{% endfor %}
</tr>
</thead>
<tbody>
{% for item in object_list %}
<tr>
{% for value in item.values %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
...
template/filters_form.html:
{% load crispy_forms_filters %}
{% load crispy_forms_tags %}
<form method="GET">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary">Search</button>
</form>
Regards,