Django - 分页和排序列表
Django - pagination and sorting list
我想在 table 中制作显示模型对象列表的视图。
我想添加排序功能。
有可能像这里一样进行排序:https://codepen.io/imarkrige/pen/vgmij in Django?
我的意思是这个列名称中的箭头。单击的位置正在将 asc 更改为 desc。
接下来的事情是在更改页面时保持排序 table。
我现在做的是排序字段,但是换页的时候不保存。
我的基本通用视图包含:
{% block pagination %}
{% if is_paginated %}
<div class="pagination">
<span class="page-links">
{% if page_obj.has_previous %}
<a href="{{ request.path }}?page= {{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="page-current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="{{ request.path }}?page={{ page_obj.next_page_number }}">next</a>
{% endif %}
</span>
</div>
{% endif %}
{% endblock %}
我的看法:
class VotesList(generic.ListView):
model = Vote
template_name = 'votes-list.html'
paginate_by = 5
def get_queryset(self):
order = self.request.GET.get('orderby', 'created_date')
new_context = Vote.objects.order_by(order)
return new_context
def get_context_data(self, **kwargs):
context = super(VotesList, self).get_context_data(**kwargs)
context['orderby'] = self.request.GET.get('orderby', 'created_date')
return context
我的模板:
{% block content %}
{% if vote_list %}
<form method="get" action="{% url 'votes-list' %}">
<p>Sortuj: <input type="text" value={{orderby}} name="orderby"/></p>
<p><input type="submit" name="submit" value="Zatwierdź"/></p>
</form>
<table id="vote_list">
<thead>
<tr>
<th>Właściciel</th>
<th>Grupa</th>
<th>Rada</th>
<th>Status</th>
<th>Pytanie</th>
<th>Odp A</th>
<th>Odp B</th>
<th>Odp C</th>
<th>Odp D</th>
<th>Odp E</th>
<th>Odp F</th>
<th>Data utworzenia</th>
<th>Data rozpoczęcia</th>
<th>Data zakończenia</th>
</tr>
</thead>
<tbody>
{% for vote in vote_list %}
<tr>
<td>{{ vote.user }}</td>
<td>{{ vote.group }}</td>
<td>{{ vote.council }}</td>
<td>{{ vote.get_status }}</td>
<td><a href="{{ vote.get_absolute_url }}">{{ vote.question }}</a></td>
<td>{{ vote.ans_a }}</td>
<td>{{ vote.ans_b }}</td>
<td>{{ vote.ans_c }}</td>
<td>{{ vote.ans_d }}</td>
<td>{{ vote.ans_e }}</td>
<td>{{ vote.ans_f }}</td>
<td>{{ vote.created_date }}</td>
<td>{{ vote.begin_date }}</td>
<td>{{ vote.finish_date }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>Brak głosowań!</p>
{% endif %}
{% endblock %}
您可以使用 javascript 进行排序,尤其是 datatablejs 可以提供帮助。然而,在页面更改之间保存排序列表的状态可能必须使用 cookie 来完成。这可能比使用 ajax 请求将用户首选项存储在 "sorting" 作为用户模型的一部分更容易。
这里是关于如何制作饼干的参考资料:
https://www.quirksmode.org/js/cookies.html
您可能想要做的是在 cookie 中保存排序顺序,然后在页面加载时读取它。阅读后,您可以这样设置排序顺序。
在分页链接的href
末尾添加order
,可以不用JS保存排序顺序。喜欢:
{% if page_obj.has_previous %}
<a href="{{ request.path }}?page= {{ page_obj.previous_page_number }}&order={{order}}">previous</a>
{% endif %}
我想在 table 中制作显示模型对象列表的视图。 我想添加排序功能。
有可能像这里一样进行排序:https://codepen.io/imarkrige/pen/vgmij in Django?
我的意思是这个列名称中的箭头。单击的位置正在将 asc 更改为 desc。 接下来的事情是在更改页面时保持排序 table。
我现在做的是排序字段,但是换页的时候不保存。
我的基本通用视图包含:
{% block pagination %}
{% if is_paginated %}
<div class="pagination">
<span class="page-links">
{% if page_obj.has_previous %}
<a href="{{ request.path }}?page= {{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="page-current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="{{ request.path }}?page={{ page_obj.next_page_number }}">next</a>
{% endif %}
</span>
</div>
{% endif %}
{% endblock %}
我的看法:
class VotesList(generic.ListView):
model = Vote
template_name = 'votes-list.html'
paginate_by = 5
def get_queryset(self):
order = self.request.GET.get('orderby', 'created_date')
new_context = Vote.objects.order_by(order)
return new_context
def get_context_data(self, **kwargs):
context = super(VotesList, self).get_context_data(**kwargs)
context['orderby'] = self.request.GET.get('orderby', 'created_date')
return context
我的模板:
{% block content %}
{% if vote_list %}
<form method="get" action="{% url 'votes-list' %}">
<p>Sortuj: <input type="text" value={{orderby}} name="orderby"/></p>
<p><input type="submit" name="submit" value="Zatwierdź"/></p>
</form>
<table id="vote_list">
<thead>
<tr>
<th>Właściciel</th>
<th>Grupa</th>
<th>Rada</th>
<th>Status</th>
<th>Pytanie</th>
<th>Odp A</th>
<th>Odp B</th>
<th>Odp C</th>
<th>Odp D</th>
<th>Odp E</th>
<th>Odp F</th>
<th>Data utworzenia</th>
<th>Data rozpoczęcia</th>
<th>Data zakończenia</th>
</tr>
</thead>
<tbody>
{% for vote in vote_list %}
<tr>
<td>{{ vote.user }}</td>
<td>{{ vote.group }}</td>
<td>{{ vote.council }}</td>
<td>{{ vote.get_status }}</td>
<td><a href="{{ vote.get_absolute_url }}">{{ vote.question }}</a></td>
<td>{{ vote.ans_a }}</td>
<td>{{ vote.ans_b }}</td>
<td>{{ vote.ans_c }}</td>
<td>{{ vote.ans_d }}</td>
<td>{{ vote.ans_e }}</td>
<td>{{ vote.ans_f }}</td>
<td>{{ vote.created_date }}</td>
<td>{{ vote.begin_date }}</td>
<td>{{ vote.finish_date }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>Brak głosowań!</p>
{% endif %}
{% endblock %}
您可以使用 javascript 进行排序,尤其是 datatablejs 可以提供帮助。然而,在页面更改之间保存排序列表的状态可能必须使用 cookie 来完成。这可能比使用 ajax 请求将用户首选项存储在 "sorting" 作为用户模型的一部分更容易。
这里是关于如何制作饼干的参考资料: https://www.quirksmode.org/js/cookies.html
您可能想要做的是在 cookie 中保存排序顺序,然后在页面加载时读取它。阅读后,您可以这样设置排序顺序。
在分页链接的href
末尾添加order
,可以不用JS保存排序顺序。喜欢:
{% if page_obj.has_previous %}
<a href="{{ request.path }}?page= {{ page_obj.previous_page_number }}&order={{order}}">previous</a>
{% endif %}