1

一般的なビューで page_obj からの結果を除外して、django-cms によって現在設定されている言語と同じ言語で公開されたエントリのみを表示しています ( http://www.django-cms.org/en/documentation/2.0/ i18n/ )。

これは問題なく動作しますが、Django ページネーション ( http://docs.djangoproject.com/en/1.2/topics/pagination/ ) のサポートを追加すると、フィルタリングされた結果が引き続きカウントされます。たとえば、英語で 3 つの結果があり、合計 10 の結果からページネーションが 2 に設定されている場合、5 つの結果ページが得られます。残りの 7 つのフィルタリングはテンプレート。

テンプレートタグを使用してテンプレート内のフィルターを操作するように Django Paginator を修正できますか?それともビューを再構築する必要がありますか? もしそうなら、どうすればそれを行うことができますか?

関連するコード:

manager.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

ビュー/エントリ.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)

urls/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 %}

ここで最善の解決策に光を当てていただければ幸いです。

4

1 に答える 1

0

魂は、最終的には、ビューを再構築することでした。この場合、大規模な再構築が必要です。

悲しみの教訓:テンプレートでフィルタリングしないでください!

于 2010-10-08T13:59:52.930 に答える