django artichels_pagination.get()缺少1个必需的位置参数:'请求'

uidvcgyl  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(84)
class artichels_pagination(ListView):

    queryset =models.article.objects.all().order_by('-upload')
    template_name = 'blog.html'
    def get(self,request):
        page_artichel = Paginator(self.queryset, 2)

        page_number = request.GET.get('page')
        page_obj = page_artichel.get_page(page_number)

        render(request,self.template_name,context={'artichel':page_obj})

字符串
这是错误:

artichels_pagination.get() missing 1 required positional argument: 'request'

ni65a41a

ni65a41a1#

尝试使用此解决方案

from django.core.paginator import Paginator

class artichels_pagination(ListView):

    queryset =models.article.objects.all().order_by('-upload')
    template_name = 'blog.html'
    context_object_name = 'articles'
    paginate_by = 5

    def get_context_data(self, **kwargs):
        context = super(artichels_pagination, self).get_context_data(**kwargs)
        articles = self.get_queryset()
        page = self.request.GET.get('page')
        paginator = Paginator(articles, self.paginate_by)
        try:
            articles = paginator.page(page)
        except PageNotAnInteger:
            articles = paginator.page(1)
        except EmptyPage:
            articles = paginator.page(paginator.num_pages)
        context['articles'] = articles

字符串

相关问题