django在模板视图中的分页

时间:2014-11-17 13:07:25

标签: python django pagination

我的观点:

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进行分页?

1 个答案:

答案 0 :(得分:0)

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