Django 中基于类的视图分页

发布于 2024-11-07 19:58:10 字数 2207 浏览 0 评论 0原文

我正在尝试对基于类的视图进行分页。这是我的视图的外观:

class IssuesByTitleView(ListView):
    context_object_name = "issue_list"

    def issues(request):
        issue_list = Issue.objects.all()
        ###### Commented out does not work ######
        # paginator = Paginator(issue_list, 24)
        # try:
        #    page = int(request.GET.get('page', '1'))
        # except ValueError:
        #   page = 1
        # try:
        #    issues = paginator.page(page)
        # except (EmptyPage, InvalidPage):
        #    issues = paginator.page(paginator.num_pages)

    def get_queryset(self):
        self.title = get_object_or_404(Title, slug=self.kwargs['title_slug'])
        return Issue.objects.filter(title=self.title).order_by('-number')
    def get_context_data(self, **kwargs):
        context = super(IssuesByTitleView, self).get_context_data(**kwargs)
        context['title'] = self.title
        return context

这是我的模型在某些上下文中的示例:

class Title(models.Model):
    CATEGORY_CHOICES = (
    ('Ongoing', 'Ongoing'),    
    ('Ongoing - Canceled', 'Ongoing - Canceled'),
    ('Limited Series', 'Limited Series'),
    ('One-shot', 'One-shot'),
    ('Other', 'Other'),
    )    
    title = models.CharField(max_length=64)
    vol = models.IntegerField(blank=True, null=True, max_length=3)
    year = models.CharField(blank=True, null=True, max_length=20, help_text="Ex) 1980 - present, 1980 - 1989.")
    category = models.CharField(max_length=30, choices=CATEGORY_CHOICES)    
    is_current = models.BooleanField(help_text="Check if the title is being published where Emma makes regular appearances.")
    slug = models.SlugField()
    class Meta:
        ordering = ['title']
    def get_absolute_url(self):
        return "/titles/%s" % self.slug        
    def __unicode__(self):

class Issue(models.Model):
    title = models.ForeignKey(Title)
    number = models.CharField(max_length=20, help_text="Do not include the '#'.")
    ...

当然,通过遵循 Django 文档,当视图由如下定义时,分页系统可以工作: def view(request):

我还想知道如何取出下一个和上一个对象。

我需要一个指向“下一期(带有名称和期号的上下文)”的链接,然后是一个“上一期”链接。请注意,仅更改模板链接与问题的下一个或上一个编号是行不通的。

所以,如果有人能帮助我,那就太好了。

I am trying to paginate my class-based view. Here is how my view looks:

class IssuesByTitleView(ListView):
    context_object_name = "issue_list"

    def issues(request):
        issue_list = Issue.objects.all()
        ###### Commented out does not work ######
        # paginator = Paginator(issue_list, 24)
        # try:
        #    page = int(request.GET.get('page', '1'))
        # except ValueError:
        #   page = 1
        # try:
        #    issues = paginator.page(page)
        # except (EmptyPage, InvalidPage):
        #    issues = paginator.page(paginator.num_pages)

    def get_queryset(self):
        self.title = get_object_or_404(Title, slug=self.kwargs['title_slug'])
        return Issue.objects.filter(title=self.title).order_by('-number')
    def get_context_data(self, **kwargs):
        context = super(IssuesByTitleView, self).get_context_data(**kwargs)
        context['title'] = self.title
        return context

Here is a sample of my models for some context:

class Title(models.Model):
    CATEGORY_CHOICES = (
    ('Ongoing', 'Ongoing'),    
    ('Ongoing - Canceled', 'Ongoing - Canceled'),
    ('Limited Series', 'Limited Series'),
    ('One-shot', 'One-shot'),
    ('Other', 'Other'),
    )    
    title = models.CharField(max_length=64)
    vol = models.IntegerField(blank=True, null=True, max_length=3)
    year = models.CharField(blank=True, null=True, max_length=20, help_text="Ex) 1980 - present, 1980 - 1989.")
    category = models.CharField(max_length=30, choices=CATEGORY_CHOICES)    
    is_current = models.BooleanField(help_text="Check if the title is being published where Emma makes regular appearances.")
    slug = models.SlugField()
    class Meta:
        ordering = ['title']
    def get_absolute_url(self):
        return "/titles/%s" % self.slug        
    def __unicode__(self):

class Issue(models.Model):
    title = models.ForeignKey(Title)
    number = models.CharField(max_length=20, help_text="Do not include the '#'.")
    ...

Of course, by following the Django docs, the pagination system works when the View is defined by something like this: def view(request):

I'm also wondering how I can pull out the next and previous objects.

I would need a link to the "next issue (with the context of the name and issue number)" and then a "previous issue" link. Please note that simply changing the template link with the next or previous number of the issue is not going to work.

So, if anyone can help me out, that would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-11-14 19:58:10

只需将 paginate_by = 20 添加到您的视图类中即可。

class IssuesByTitleView(ListView):
    context_object_name = "issue_list"
    paginate_by = 20

    #More stuff here..

Just add paginate_by = 20 to you view class.

class IssuesByTitleView(ListView):
    context_object_name = "issue_list"
    paginate_by = 20

    #More stuff here..
一梦等七年七年为一梦 2024-11-14 19:58:10

正如 Evan Porter 所评论的那样,您可以使用 page_obj 上下文变量来访问 number、paginatior.num_pages、has_next、has_previous 。这就是从 Django 1.4.1 升级到 1.7、object_list 升级到 ListView 后将我从 KeyError['page'] 中拯救出来的原因

Just like Evan Porter has commented, you can make use of the page_obj context variable to access number, paginatior.num_pages, has_next, has_previous. This is what just saved me from the KeyError['page'] after upgrading from Django 1.4.1 to 1.7, object_list to ListView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文