Я показываю данные на веб-странице и хочу перенести этот код, чтобы использовать таблицу, которую я создал в файлеtables.py. Я не могу понять, как это сделать, не сломав фильтр.
просмотры.py
def PlatListView(request):
queryset = Plat.objects.all().values('id', 'description','status', 'phase__number','phase','schedule_found').annotate(lot_count=Sum('phase__lot_count')).order_by('description')
f = PlatFilter(request.GET, queryset=queryset)
return render(request, 'blog/filtertable2.html', {'filter': f})
фильтры.py
class PlatFilter(django_filters.FilterSet):
community = ModelChoiceFilter(queryset=Community.objects.all())
таблицы.py
import django_tables2 as tables
class PlatTable(tables.Table):
id = tables.Column()
description = tables.Column()
status = tables.Column()
phase__number = tables.Column()
lot_count = tables.Column()
schedule_found = tables.Column()
class Meta:
ordering = 'description'
#model = Plat
filtertable2.html
{% extends "blog/base.html" %}
{% block content %}
{% load bootstrap4 %}
<form method = "get">
{{ filter.form.as_p }}
<input type = "submit" />
</form>
<table>
<tr>
<th>Description</th>
<th>Status</th>
</tr>
{% for obj in filter.qs %}
<tr>
<td> {{ obj.description }}</td>
<td>{{ obj.status }}</td>
</tr>
{% endfor %}
</table>
{% endblock content %}
@WillemVanOnsem добавил html





Я не вижу особых причин для использования Values QuerySet, т.е. values(). Вы можете просто аннотировать значение с помощью набора запросов:
def plat_list_view(request): # using snake_case when defining a method name.
queryset = Plat.objects.annotate(lot_count=Sum('phase__lot_count')).order_by('description')
f = PlatFilter(request.GET, queryset=queryset)
return render(request, 'blog/filtertable2.html', {'filter': f})
В вашем представлении вы строите таблицу с данными из фильтра:
def PlatListView(request):
queryset = Plat.objects.annotate(
lot_count=Sum('phase__lot_count')
).order_by('description')
f = PlatFilter(request.GET, queryset=queryset)
table = PlatTable(data=f.qs)
return render(request, 'blog/filtertable2.html', {'filter': f, 'table': table})Затем вы можете отобразить таблицу с помощью:
{% extends "blog/base.html" %}
{% block content %}
{% load bootstrap4 %}
{% load render_table from django_tables2 %}
<form method = "get">
{{ filter.form.as_p }}
<input type = "submit" />
</form>
{% render_table table %}
{% endblock content %}понял, спасибо. И как включить фильтр в шаблон?
@MattWilson: так же, как и раньше. Забыл об этом. Обновил ответ.
@MattWilson: также сделал опечатку в представлении, filter, конечно, по-прежнему относится к f, а не к f.qs.
Как вы рендерите шаблон? Можете ли вы предоставить соответствующие части шаблона?