我使用Django的無盡分頁搜索結果的當前數量,我想創建頂部只是在陳述一個消息,例如:Django的無盡分頁 - 顯示結果(未頁)
showing 20 results of 124
甚至更好:
showing results 20 to 40 of 124
我看不出有什麼辦法讓在endless pagination documentation結果的當前數量。你會如何做到這一點?
我使用Django的無盡分頁搜索結果的當前數量,我想創建頂部只是在陳述一個消息,例如:Django的無盡分頁 - 顯示結果(未頁)
showing 20 results of 124
甚至更好:
showing results 20 to 40 of 124
我看不出有什麼辦法讓在endless pagination documentation結果的當前數量。你會如何做到這一點?
我會做如下:
從數據庫統計條目總數和傳遞給模板,這將是124
。然後使用頁面限制來限制每頁總條目的數量。然後計數並顯示你想要的。
解決方案:
使用PAGINATE模板標籤,電話{%get_pages%},並設定總的結果是這樣一個變量之後:
{% paginate items %}
{% get_pages %}
{% with total=pages.total_count %}
<div>Total results: {{ total }}</div>
{% for item in items %}
...
{% endfor %}
{% endwith %}
我已經寫我自己的自定義標籤通過使用現有的標籤此事:
from endless_pagination import (
settings,
utils,
)
@register.inclusion_tag("endless_pagination/show_more.html", takes_context=True)
def show_more_with_counts(context, per_page_count, total_count, first_page_count=0,
verb='enteries', loading=settings.LOADING, show_total_in_end=True):
# This template tag could raise a PaginationError: you have to call
# *paginate* or *lazy_paginate* before including the showmore template.
data = utils.get_data_from_context(context)
page = data['page']
# show the template only if there is a next page
if page.has_next():
request = context['request']
page_number = page.next_page_number()
# Generate the querystring.
querystring_key = data['querystring_key']
querystring = utils.get_querystring_for_page(
request, page_number, querystring_key,
default_number=data['default_number'])
curr_page_num = int(request.GET.get(querystring_key, 1))
if curr_page_num == 1:
if first_page_count:
start = first_page_count + 1
else:
start = per_page_count + 1
else:
if first_page_count:
start = (curr_page_num * per_page_count) - first_page_count
else:
start = (curr_page_num * per_page_count) + 1
end = (per_page_count + start) - 1
if end > total_count:
end = total_count
label = 'Load %(start)s to %(end)s of %(total)s %(verb)s' % {
'start': start, 'end': end, 'total': total_count, 'verb': verb}
return {
'label': label,
'loading': loading,
'path': data['override_path'] or request.path,
'querystring': querystring,
'querystring_key': querystring_key,
'request': request,
'show_total_in_end': show_total_in_end,
}
else:
if total_count > 0:
return {
'label': 'Showing %(start)s of %(end)s %(verb)s' % \
{'start': total_count, 'end': total_count, 'verb': verb},
'show_total_in_end': show_total_in_end,
}
else:
return {}
而且我有以下show_more.html
模板:
{% load i18n %}
{% if querystring %}
<div class="endless_container">
<a class="endless_more" href="{{ path }}{{ querystring }}"
rel="{{ querystring_key }}" style="font-size: 11px; color: #c13923;">{{ label }}</a>
<div class="endless_loading" style="display: none;">{{ loading|safe }}</div>
</div>
{% elif show_total_in_end %}
<a href="#" style="text-decoration: none; color: #999; cursor:default; font-size: 11px;" onclick='return false;'>{{ label }}</a>
{% endif %}
如何使用:
{% show_more_with_counts 10 qs_count verb='users' %}
# it will say `Load 1 to 10 of 100 users` for first page
# it will say `Load 11 to 20 of 100 users` for 2nd page, and so on
你要通過per_page_count
,使用的查詢集或列表total_count
和verb
對象的總數。
人們傾向於過分複雜這一點。我發現current_start_index和current_end_index函數在源代碼中有很好的文檔記錄(但不是在實際的文檔中)。
下面是你將如何使用它們來實現這一目標。
{{pages.current_start_index}}通過{{pages.current_end_index}}的{{pages.total_count}}結果
希望這可以幫助別人:d
你找到了一個解決方案? –