Django中的分页式分类视图 [英] Paginate Class-based View in Django

查看:114
本文介绍了Django中的分页式分类视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图分类我的课堂观点。这是我的看法如何:

  class IssuesByTitleView(ListView):
context_object_name =issue_list

def问题(请求):
issue_list = Issue.objects.all()
######注释不起作用######
#paginator = Paginator(issue_list,24)
#try:
#page = int(request.GET.get('page','1'))
#除了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)
c ontext ['title'] = self.title
return context

我的模型为某些上下文:

 类标题(models.Model):
CATEGORY_CHOICES =(
正在进行,正在进行),
(正在进行中取消,正在进行中取消),
(限定系列,有限系列),
- $ a
('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 - 。)
category = models.CharField(max_length = 30,choices = CATEGORY_CHOICES)
is_current = models.BooleanField(help_text =检查Emma是否定期出现时正在发布标题)
slug = models.SlugField()
class Meta:
订单= ['title']
def get_absolute_url(self):
return/ titles /%s%self.slug
def __unicode __(self):

类问题(models.Model):
title = models.ForeignKey(Title)
number = models.CharField(max_length = 20,help_text =不要包含'#' $ b ...

当然,通过跟踪Django文档,分页系统在View由这样定义: def view(request):



我也想知道如何拉出现下一个和以前的对象。



我需要一个链接到下一个问题(带有上下文的名称和问题号),然后是上一个问题链接。请注意,只需更改下一个或上一个问题数量的模板链接即可。



所以,如果有人可以帮助我,那将是巨大的。

解决方案

只需将 paginate_by = 20 添加到您的视图类。 p>

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

#更多的东西在这里..


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.

解决方案

Just add paginate_by = 20 to you view class.

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

    #More stuff here..

这篇关于Django中的分页式分类视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆