简体   繁体   中英

post_list() missing 1 required positional argument: 'request'

I Have

View.py

class mainView(View):
    template_name = "search.html"

    def error_page(self,request):
        return render(request, 'error_page.html')

    def getQuery(self,query,):
        posts_list = Galeria.objects.filter(
            Q(nazwa__icontains=query) | Q(opis__icontains=query)
        ).distinct()
        return(posts_list)

    def postPagination(self,paginator,page):
        try:
            posts = paginator.page(page)
        except PageNotAnInteger:
            posts = paginator.page(1)
        except EmptyPage:
            posts = paginator.page(paginator.num_pages)
        return posts

    def post_list(self,request, category_slug=None, **kwargs):
            posts_list = Galeria.objects.all()
            query = request.GET.get('q')
            if query:
                posts_list = self.getQuery(query)
            ile = posts_list.count
            paginator = Paginator(posts_list, 10)  # 10 posts per page
            page = request.GET.get('page')
            posts = self.postPagination(paginator, page)
            category = None
            categories = Category.objects.all()
            if category_slug:
                category = get_object_or_404(Category, slug=category_slug)
            categories_ile = Galeria.objects.values('category__name').annotate(count=Count('category__name'))
            context = {
                'posts': posts,
                'ile': ile,
                'categories': categories,
                'category': category,
                'categories_ile': categories_ile}
            return render(request, "search.html", context)
...

url.py


urlpatterns = [
              
                  path('searchbar/', mainView.post_list, name="searchbar"),
...
]

Why I get error "post_list() missing 1 required positional argument: 'request'"? I trying everything and I read documentation on this page https://docs.djangoproject.com/en/3.2/topics/class-based-views/intro/ and still nothing.

You are using class-based views the wrong way. Typically you implement the get , post , put , etc. methods to return a HTTP response, furthermore you should use .as_view() to wrap the class-based view into a function:

class mainView(View):
    # …

    #   ↓ use get, not post_list
    def get(self,request, category_slug=None, **kwargs):
            posts_list = Galeria.objects.all()
            query = request.GET.get('q')
            if query:
                posts_list = self.getQuery(query)
            ile = posts_list.count
            paginator = Paginator(posts_list, 10)  # 10 posts per page
            page = request.GET.get('page')
            posts = self.postPagination(paginator, page)
            category = None
            categories = Category.objects.all()
            if category_slug:
                category = get_object_or_404(Category, slug=category_slug)
            categories_ile = Galeria.objects.values('category__name').annotate(count=Count('category__name'))
            context = {
                'posts': posts,
                'ile': ile,
                'categories': categories,
                'category': category,
                'categories_ile': categories_ile}
            return render(request, "search.html", context)

and in the urls.py :

urlpatterns = [
    #                          ↓ use .as_view()
    path('searchbar/', mainView.as_view(), name="searchbar"),
    # …
]

That being said, it looks like you are implementing your own version of a ListView [Django-doc] , it might be better to start with a ListView and override some methods like .get_queryset(…) to implement your view. This will also remove a lot of boilerplate code like pagination, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM