django-cms (http://www.django-cms.org/en/documentation/2.0/i18n/)가 현재 설정 한 languge와 같은 언어로 게시 된 항목 만 표시하기 위해 제 일반적인 view에서 내 page_obj의 결과를 필터링하고 있습니다.템플릿의 Django paginator에 대한 필터 결과
이 작동하지만 장고 페이지 매기기 (http://docs.djangoproject.com/en/1.2/topics/pagination/)에 대한 지원을 추가하면 여전히 필터링 된 결과가 계산됩니다. 그래서, 예를 들어 영어로 세 가지 결과가 나오면 총 10 개의 결과와 페이지 매김이 2로 설정됩니다. 나머지 5 개의 결과 페이지는 5 개뿐입니다. 대부분은 공백이므로 나머지 7 개는 필터링됩니다. 템플릿.
템플릿 태그를 사용하여 템플릿의 필터로 작업 할 장고 Paginator를 개정 할 수 있습니까, 아니면 뷰를 재구성해야합니까? 그렇다면 어떻게해야합니까?
관련 코드 :
managers.py
def update_queryset(view, queryset, queryset_parameter='queryset'):
'''Decorator around views based on a queryset passed in parameter, which will force the update of the queryset before executing the view.
Related to issue: http://code.djangoproject.com/ticket/8378'''
def wrap(*args, **kwargs):
#Regenerate the queryset before passing it to the view.
kwargs[queryset_parameter] = queryset()
return view(*args, **kwargs)
return wrap
인/entries.py
from django.views.generic.list_detail import object_list
from cmsplugin_publisher.models import Entry
from cmsplugin_publisher.managers import update_queryset
entry_index = update_queryset(object_list, Entry.published.all)
된 URL/entries.py
entry_list.html{% block content %}
{% for object in object_list %}
{% ifequal object.language current_language %}
..
{% endifequal %}
{% endfor %}
{% if is_paginated %}
<ul id="pagination">
{% if page_obj.has_previous %}
{% ifnotequal page_obj.start_index 1 %}<li><a href="../" title="{% trans 'View Latest Entries' %}">{% trans 'Latest Entries' %}</a></li>{% endifnotequal %}
{% ifequal page_obj.previous_page_number 1 %}{% endifequal %}
{% ifnotequal page_obj.previous_page_number 1 %}
<li><a href="../{{ page_obj.previous_page_number }}/" title="{% trans 'View Earlier Entries' %}">{% trans 'Earlier Entries' %}</a></li>
{% endifnotequal %}
{% else%}
{% endif %}
<li>{% trans 'Page' %} {{ page_obj.start_index }} {% trans 'of' %} {{ paginator.num_pages }} {% trans 'Entries' %}</li>
{% if page_obj.has_next %}
{% ifnotequal page_obj.start_index 1 %}
<li><a href="../{{ page_obj.next_page_number }}/" title="{% trans 'View Older Entries' %}">{% trans 'Older Entries' %}</a></li>
{% endifnotequal %}
{% ifequal page_obj.start_index 1 %}
<li><a href="{{ page_obj.next_page_number }}/" title="{% trans 'View Older Entries' %}">{% trans 'Older Entries' %}</a></li>
{% endifequal %}
{% else%}
{% endif %}
</ul>
{% endif %}
에서
from django.conf.urls.defaults import *
from cmsplugin_publisher.models import Entry
from cmsplugin_publisher.settings import PAGINATION, ALLOW_EMPTY, ALLOW_FUTURE
entry_conf_list = {'queryset': Entry.published.all(), 'paginate_by': PAGINATION}
entry_conf = { 'queryset': Entry.published.all(),}
entry_conf_detail = entry_conf.copy()
entry_conf_detail['queryset'] = Entry.objects.all()
urlpatterns = patterns('cmsplugin_publisher.views.entries',
url(r'^$', 'entry_index', entry_conf_list, name='cmsplugin_publisher_entry_archive_index'),
url(r'^(?P<page>[0-9]+)/$', 'entry_index', entry_conf_list, name='cmsplugin_publisher_entry_archive_index_paginated'),
)
urlpatterns += patterns('django.views.generic.list_detail',
url(r'^(?P<slug>[-\w]+)/$', 'object_detail', entry_conf_detail, name='cmsplugin_publisher_entry_detail'),
)
나는 너희들이 여기에 최고의 솔루션을 던질 수있는 모든 빛을 감사하겠습니다.
왜 템플릿이 아닌 쿼리 자체에서 필터를 사용하지 않는 것입니까? Django는 템플릿에서 사용하는 쿼리 결과를 기반으로 페이지 매김 링크를 만들고, 템플릿에서 변경하면 좋은 생각이 아닌 것 같습니다. –