1

我有模型:

class Lesson(models.Model):
    title = models.CharField(max_length=200)
    lessoncontent = RichTextField()
    category = models.ForeignKey(CategoryLesson)

并查看:

class LessonDetailView(DetailView):
    model = Lesson
    paginate_by = 1
    template_name = 'lessons/lesson_detail.html'

并且还有 ListView 用于列出课程

我如何对我的课程进行分页,例如 - <<< 上一课 | 下一课 >>> ?

4

2 回答 2

1

将其称为分页是您正在做的错误。对于每个位置,您希望能够根据某些标准导航到上一个和下一个位置。向您的 Location 模型添加两种方法——一种用于上一个,一种用于下一个——它们返回适当的条目。

请参阅 Mezzanine cms 中的此代码,该代码根据发布日期获取上一个和下一个 - https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/core/models.py#L243

对应模板代码 - https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/blog/templates/blog/blog_post_detail.html#L91

{% block blog_post_previous_next %}
<ul class="pager">
{% with blog_post.get_previous_by_publish_date as previous %}
{% if previous %}
<li class="previous">
    <a href="{{ previous.get_absolute_url }}">&larr; {{ previous }}</a>
</li>
{% endif %}
{% endwith %}
{% with blog_post.get_next_by_publish_date as next %}
{% if next %}
<li class="next">
    <a href="{{ next.get_absolute_url }}">{{ next }} &rarr;</a>
</li>
{% endif %}
{% endwith %}
</ul>
{% endblock %}
于 2013-09-09T06:20:29.687 回答
0

我认为最简单的方法是使用 ListView 而不是 DetailView。

于 2013-09-09T05:37:09.810 回答