我在我的项目中使用Django的评论框架来处理电影的评论:
我的my_comments_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来获取我的评论系统分页请求?
我问这个是因为我没有使用任何views.py来生成我的评论(如django docs中所述)
如何在评论中添加分页?
我尝试了以下内容:
帮助我对我的评论进行分页。
答案 0 :(得分:1)
在views.py文件中
def CommentView(request):
allComments = UserExperience.objects.all()
paginator = Paginator(comments, 10)
comment = paginator.page(1)
“评论”的值将是[0 ... 9]评论。