【发布时间】:2022-01-04 16:39:42
【问题描述】:
我正在尝试使用一些在线代码将分页器添加到我的网站,但出现此错误 返回 len(self.object_list) TypeError:“方法”类型的对象没有 len()
Views.py
定义样本(请求):
WAllPAPER_PER_PAGE = 2 wallpapers = Wallpaper.objects.all page = request.GET.get('page', 1) wallpaper_paginator = Paginator(wallpapers, WAllPAPER_PER_PAGE) try: wallpapers = wallpaper_paginator.page(page) except EmptyPage: wallpapers = wallpaper_paginator.page(wallpaper_paginator.num_pages) except: wallpapers = wallpaper_paginator.page(WAllPAPER_PER_PAGE) context = {"wallpapers": wallpapers, 'page_obj': wallpapers, 'is_paginated': True, 'paginator': wallpaper_paginator} return render(request, "Wallpaper/sample.html", context )
Models.py
class Wallpaper(models.Model):
name = models.CharField(max_length=100, null=True)
size = models.CharField(max_length=50, null=True)
pub_date = models.DateField('date published', null=True)
resolution = models.CharField(max_length=100, null=True)
category = models.ManyToManyField(Category)
tags = models.ManyToManyField(Tags)
Device_Choices = [
('PC', 'pc'),
('mobile', 'mobile')
]
Devices = models.CharField(max_length=20,choices=Device_Choices, default= 'PC')
image = models.ImageField(upload_to='Wallpaper/Images/', default="")
def __str__(self):
return self.name
【问题讨论】:
标签: python html django django-views django-queryset