gpt4 book ai didi

python - django 在模板中实现过滤器

转载 作者:行者123 更新时间:2023-11-28 23:09:39 26 4
gpt4 key购买 nike

我正在构建一个界面(类似于 phpmyadmin),但对用户更友好...我已经导入了所有表格,但现在我想在每个表格上添加排序、筛选和搜索。

这是我的表/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 }}">&laquo;</a></li>
{% else %}
<li class="disabled"><span>&laquo;</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 }}">&raquo;</a></li>
{% else %}
<li class="disabled"><span>&raquo;</span></li>
{% endif %}
</ul>
{% endif %}


{% endblock %}

我想在此模板中进行排序、过滤和搜索,这对我拥有的所有表格都是动态的。 (所有表的结构相同)

我确实创建了一个单独的搜索模板...但这只是为了测试

提前感谢您的帮助

最佳答案

实际上你需要在获取对象后在 View 中编写过滤器搜索排序然后你需要传入模板。

当然,所有这些searchsortfilters 都有内置函数,但您需要在 View 中处理它们。

django 模板标签

还有用于其他用途的模板标签。你可以上Django Bultin Template Tags看看.

关于python - django 在模板中实现过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46321034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com