1
我建立了一個界面(類似於phpmyadmin),但更友好...我已經導入了所有表格,但現在我想在每個表格上添加排序,篩選和搜索。django在模板中實現過濾器
這是我的表/ views.py
def table_base(request):
table_name = Crawledtables._meta.db_table
list_tables = Crawledtables.objects.order_by('id')
return render(request, 'tables/table_base.html', {'table_name': table_name,
'list_tables': list_tables})
class AboutDetail(DetailView):
model = Crawledtables
pk_url_kwarg = 'table_id'
template_name = 'tables/table_list.html'
def __init__(self, **kwargs):
super(AboutDetail, self).__init__(**kwargs)
def get_object(self):
if 'table_id' not in self.kwargs:
return Crawledtables.objects.get(id=1)
else:
return Crawledtables.objects.get(id=self.kwargs['table_id'])
def addview(request, table_id):
table_name = Crawledtables.objects.get(id=table_id)
tbl_details = "SELECT * FROM " + table_name.name
tbl_detail = AllTables.objects.raw(tbl_details)
paginator = Paginator(list(tbl_detail), 100)
page = request.GET.get('page')
try:
details = paginator.page(page)
except PageNotAnInteger:
details = paginator.page(1)
except EmptyPage:
details = paginator.page(paginator.num_pages)
crawled_tables = AllTablesFilter(request.GET, queryset=tbl_detail)
return render(request, 'tables/table_list.html', {'tbl_name': table_name,
'details': tbl_detail,
'filter': crawled_tables,
'detail_page': details})
def GeneralSearch(request):
table_list = Crawledtables.objects.all()
crawled_tables = GeneralFilter(request.GET, queryset=table_list)
return render(request, 'tables/table_search.html', {'filter': crawled_tables})
def AllTablesSearch(request, table_id):
table_name = Crawledtables.objects.get(id=table_id)
tbl_details = "SELECT * FROM " + table_name.name
table_list = AllTables.objects.raw(tbl_details)
# table_list = Crawledtables.objects.all()
crawled_tables = AllTablesFilter(request.GET, queryset=table_list)
return render(request, 'tables/alltables_search.html', {'tbl_name': table_name,
'filter': crawled_tables})
,這是我的表/ filter.py
class GeneralFilter(django_filters.FilterSet):
class Meta:
model = Crawledtables
fields = ['name', 'date']
class AllTablesFilter(django_filters.FilterSet):
class Meta:
model = AllTables
fields = ['id','title', 'url','description']
和我table_list.html
{% extends 'base.html' %}
{% block content %}
{% load app_filter %}
<br>
<h1> {{tbl_name|split:"_"}} {{ tbl_name.date|split:"-" }} Data</h1>
<table class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Url</th>
<th>Descriptions</th>
</tr>
</thead>
{% for lists in details %}
<tr>
<td>{{ lists.id }}</td>
<td>{{ lists.title }}</td>
<td><a href="{{ lists.url }}" target="_blank">{{ lists.url }}</a></td>
<td>{{ lists.description }}</td>
</tr>
{% endfor %}
</table>
{% if detail_page.has_other_pages %}
<ul class="pagination">
{% if detail_page.has_previous %}
<li><a href="?page={{ detail_page.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in detail_page.paginator.page_range %}
{% if detail_page.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if detail_page.has_next %}
<li><a href="?page={{ detail_page.next_page_number }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
{% endblock %}
我想有排序,過濾和搜索這個模板,這將是我擁有的所有表的動態。 (所有的表結構相同)
我沒有創建一個單獨的搜索模板......但那只是用於測試
預先感謝您的幫助
我應該寫過濾器,搜索和排序在同一個函數(addview),然後當我渲染..我還包括這3件事情...或者我創建一個新的功能,只有這三件事separte和調用與函數addview相同的模板? – Omega
您可以一直這樣做,直到您正確地做到這一點。 –
k謝謝你的幫助 – Omega