2

django-endless-paginationのドキュメントを読むと、 @page_template() デコレーターを使用するだけで、Ajaxページング機能をクラスベースのビューに拡張できると書かれています... :

class ExtendedListView(ListView):
    template_name = 'global_template.html'

    @method_decorator(@page_template('path_to_updatable_content_only_template'))
    def dispatch(self, *args, **kwargs):
        return super(ExtendedListView, self).dispatch(*args, **kwargs)

ビュー関数はエラーを出力しませんが、別のページに移動すると、デコレーターで定義されたテンプレートではなく、ターゲットに「global_template」が読み込まれます。

この実装が実際に機能するかどうかを誰かが知っていて、私が間違いを犯している場合は、指摘してください。正しい方法で使用できれば幸いです。

私はなんとか回避策を思いついたので、誰かがこの同じ問題を抱えていて、これに対する準拠した回答がない場合は、これを行うことができます:

class ExtendedListView(ListView):
    template_name='global_template_path'

    ''' 
    render_to_response ¿hack? so that i can render only the updatable DOM part template
    '''
    def render_to_response(self, context):
        if self.request.is_ajax():
            self.template_name = 'path_to_updatable_content_only_template'
            return super(ExtendedListView, self).render_to_response(context)
        else:
            return super(ExtendedListView, self).render_to_response(context)

乾杯!

4

2 に答える 2

3

正式には、AjaxListView を使用してこのタスクを実行できます。

# views.py
from endless_pagination.views import AjaxListView    
class BookView(AjaxListView):
    context_object_name = "books"
    model = Book

    template_name = 'books.html'
    page_template = 'books_list.html'

book.html で:

{% extends 'base.html' %}


{% block js %}
    {{ block.super }}
    <script src="/static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/js/endless.js" type="text/javascript" charset="utf-8"></script>
{% endblock %}

{% block content %} 

<div class="endless_page_template">

    {% include page_template %}
</div>

{% endblock %}

これは books_list.html です

{% load endless %}

{% paginate books %}

{% for book in books %} 
    {{ book.title }}
{% endfor %}

<div class="pagination">

    {% show_pages %}
</div>
于 2012-06-16T08:50:54.007 に答える
1

Ajax の実装がどのように機能するかは、実際には非常に複雑です。汎用ビュー、Ajax、および複数のページネーションを使用したかったため、独自のソリューションを作成する必要がありました。それを機能させる方法を理解するために、サンプル、django-endless-pagination デコレーター、および Django のビュー自体からコードをリバース エンジニアリングする必要がありました。独自のソリューションを展開する過程で、少し単純化しましたが、おそらくさらに単純化できます。おそらく、これは他の誰かに役立つかもしれません:

class SearchView(View):
    """
    Based on code from django-endless-pagination, modified for multiple
    pagination views on the same page 
    """

    template = 'app/search.html'
    page_templates = {'object1Page': 'app/search_object1_pane.html',
    'object2Page': 'app/search_object2_pane.html'}

    def get_context_data_and_template(self, **kwargs):
        context = {'params': kwargs}

        # Check whether AJAX has made a request, if so, querystring_key will be
        # set, identifying which paginator to render
        querystringKey = self.request.REQUEST.get('querystring_key')
        template = self.page_templates.get(querystringKey)
        # switch template on ajax requests
        if not (self.request.is_ajax() and template):
            template = self.template    
        context['page_template'] = template

        # Always generate the QuerySets that will be paginated
        if self.request.GET['query']:
            searchTerm = self.request.GET['query']
        else:
            searchTerm = kwargs['search']

        # *** Insert your search code here ***   

        context['object1Results'] = object1QuerySet
        context['object2Results'] = object2QuerySet

        extraContext = self.kwargs.get("extra_context", {})
        context.update(extraContext)

        return context, template

    def get(self, request, *args, **kwargs):
        context, template = self.get_context_data_and_template(**kwargs)
        return TemplateResponse(request=self.request,
            template=template,
            context=context)
于 2012-09-27T08:00:24.620 に答える