我在一般视图中过滤了page_obj中的结果,只显示使用与django-cms(http://www.django-cms.org/en/documentation/2.0/i18n/)当前设置的语言相同的语言发布的条目。
这样可以正常工作,但是添加对Django分页(http://docs.djangoproject.com/en/1.2/topics/pagination/)的支持会导致过滤后的结果仍然被计算在内。所以,例如当英语有三个结果时,从总共十个结果和分页设置为2,我将获得5个结果页面,其中大多数当然是空白的,因为其余七个的过滤是在模板。
我可以使用模板标记修改Django 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
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'),
)
在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 %}
我很感激你们可以在这里找到最好的解决方案。
答案 0 :(得分:0)
最终,解决方案是重建视图。在这种情况下需要进行广泛的重建。
故事的道德:不要过滤模板!