1

I am using Django's Comments Framework in my project to handle comments of a movie :

My my_comments_app/models.py looks like this :

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

and my my_comment_app/forms.py looks like :

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)

Where and when should i add paginate_by = 4 to get my comments system paginate requests ?

I am asking this because i am not using any views.py to generate my comments (as described in django docs)

How do i add pagination to the comments ?

I have tried the following:

  • Pagination using views.py. By creating a view and using its
  • Seeing that I am somehow using Class Based View in my UserExperience class (of which I am not sure, I might be completely wrong..) I have tried this answer too. Which is about pagination for Class Based Views.

none of them worked.

Help me paginate over my comments.

Community
  • 1
  • 1
madil
  • 35
  • 6

1 Answers1

2

In the views.py file

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

The value of "comment" will be [0...9] comment.

iraycd
  • 892
  • 1
  • 8
  • 23
  • I've tried it, din't work. because django.contrib.comments does not use any views.py to generate the comments. It only has a models.py if we want to add any new field and a form. – madil May 24 '13 at 04:56
  • Paginator is used to make the model into pages. It won't be implemented paginate_by = 4 should be used in the views.py [See this reference](http://stackoverflow.com/questions/5907575/how-do-i-use-pagination-with-django-class-based-generic-listviews) – iraycd May 24 '13 at 18:45
  • i have seen that. this link in the question above. – madil May 24 '13 at 20:39
  • Please add the views.py if you can and i'll try to change it. – iraycd May 25 '13 at 11:46
  • y cant you just use filter to work out the pagination in the templates – Vishnu667 Jun 30 '13 at 11:14
  • @Vishnu667 `views.py` take the entire code of the template with `render_to_response` and render's it. So, instead of making the rendering harder we write all the logic in the views.py and template (can) have only few logical or looping statements. Some time writing in the views.py also is useful to returning json. Best way is to use views.py – iraycd Jul 15 '13 at 13:23