【问题标题】:django pagination in template view模板视图中的django分页
【发布时间】:2014-11-17 21:26:33
【问题描述】:

我的看法:

class GeneralUserProfileView(TemplateView):

    context = {}
    model = GeneralUser
    template_name = "gnu_profile.html"

    def get(self, request, pk, username, **kwargs):
        self.pk = pk
        self.username =  username
        return super(GeneralUserProfileView, self).get(request, pk, **kwargs)

    def get_context_data(self, **kwargs):

        context =  super(GeneralUserProfileView, self).get_context_data(**kwargs)
        context['basic_info'] = GeneralUser.objects.get(pk=self.pk)
        context['posted_questions'] = Question.objects.filter(user__id=self.pk)
        return context

我有模板:

    <div id="activitylog">
            <h2><u>Activity Log</u></h2><br/>
            <ul>
            {% for post in posted_questions %}
                <font size="2">
                <li type="disc"><a href="{% url "question-detail" post.id post.category.id %}">{{post.title|truncatewords:12}}</a></li>
                </font>
                <!--Pagination here-->
            {% endfor %}
    <div class="pagination">
    <span class="step-links">
        {% if posted_questions.has_previous %}
            <a href="?page={{ posted_questions.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ posted_questions.number }} of {{ posted_questions.paginator.num_pages }}.
        </span>

        {% if posted_questions.has_next %}
            <a href="?page={{ posted_questions.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

            </ul>
    </div>      

这里我没有分页。我已经搜索过了,但所有示例都是针对 ListView 的。 我是否也有可能获得 TemplateView 的分页??

【问题讨论】:

  • 您为什么不想为问题对象使用列表视图并将其余部分添加为额外的上下文?
  • 这就是问题,如果在某些情况下我想对 TemplateView 使用分页..
  • 您是否找到任何解决方法来为 TemplateView 使用分页。我想了解这方面的一些见解

标签: python django pagination


【解决方案1】:

从 django 1.5 开始,您可以使用 MultipleObjectsMixin 扩展 TemplateView: https://docs.djangoproject.com/en/1.5/ref/class-based-views/mixins-multiple-object/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2013-06-04
    • 2016-09-21
    • 2012-05-26
    • 2023-03-07
    • 2013-12-25
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多