【问题标题】:How to add pagination to django.contrib.comments?如何为 django.contrib.comments 添加分页?
【发布时间】:2013-05-18 09:29:03
【问题描述】:

我在我的项目中使用 Django 的评论框架来处理电影的 cmets:

我的 my_cmets_app/models.py 看起来像这样:

from django.db import models
from django.contrib.comments.models import Comment

class UserExperience(Comment):
    experience = models.CharField('User Experience', max_length=20)

    # paginate_by = 4 | This does not work !!

    def __unicode__(self):
        return self.user.username

我的 my_comment_app/forms.py 看起来像:

from django import forms
from .models import UserExperience
from django.contrib.comments.forms import CommentForm

from movies.models import Movie
from django.contrib.comments.moderation import CommentModerator, moderator


class UserExperienceForm(CommentForm):
    experience = forms.CharField(max_length=20)

    def get_comment_model(self):
        return UserExperience

    def get_comment_create_data(self):
        data = super(UserExperienceForm, self).get_comment_create_data()
        data['experience'] = self.cleaned_data['experience']
        return data

class MovieCommentModerator(CommentModerator):
    email_notification = False
    auto_close_field = "start_date"
    close_after = 31

moderator.register(Movie, MovieCommentModerator)

我应该在何时何地添加 paginate_by = 4 以获取我的 cmets 系统分页请求?

我问这个是因为我没有使用任何 views.py 来生成我的 cmets(如 django 文档中所述)

如何为 cmets 添加分页功能?

我尝试了以下方法:

  • Pagination using views.py。通过创建视图并使用其
  • 看到我在我的 UserExperience 类中以某种方式使用基于类的视图(我不确定,我可能完全错了..)我也尝试过this 的答案。这是关于基于类的视图的分页。

他们都没有工作。

帮我对我的 cmets 进行分页。

【问题讨论】:

    标签: django django-comments django-pagination


    【解决方案1】:

    在views.py文件中

    def CommentView(request):
        allComments = UserExperience.objects.all()
        paginator = Paginator(comments, 10)
        comment = paginator.page(1)
    

    “comment”的值为 [0...9] 评论。

    【讨论】:

    • 我试过了,不行。因为 django.contrib.cmets 不使用任何 views.py 来生成 cmets。如果我们想添加任何新字段和表单,它只有一个 models.py。
    • Paginator 用于将模型制作成页面。不会实现 paginate_by = 4 应该在views.py中使用See this reference
    • 我已经看到了。上述问题中的此链接。
    • 如果可以的话,请添加views.py,我会尝试更改它。
    • 你不能只使用过滤器来计算模板中的分页
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2013-06-19
    相关资源
    最近更新 更多