使用带有django-endless-pagination的过滤器 [英] using filters with django-endless-pagination

查看:120
本文介绍了使用带有django-endless-pagination的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django endles-pagination以无限滚动方式加载页面.我也有一些过滤器,可以根据条件过滤数据(例如,根据价格进行价格滑块过滤).现在,当页面加载时,过滤器现在仅从加载的页面中进行过滤,尽管我希望它从已经或将要加载的所有页面中进行过滤.有没有办法做到这一点(通过发出一些ajax请求或其他方式)?

I'm using Django endles-pagination to load the pages in infinite scroll. I also have some filters that filter the data according to the criteria (for eg, price slider filtering according to price). Now when the page loads, the filter right now filters only from the page loaded, though I want it to filter it from all the pages that have been or are to be loaded. Is there a way to do this (by making some ajax request or something)?

任何对此的帮助都会很棒.非常感谢.

Any help on this would be great. Thanks a lot.

推荐答案

要过滤数据,必须在请求过滤查询的视图中重新定义get_queryset()方法.

To filter the data you have to redefine get_queryset() method in the views requesting the filtered query.

例如,我请求模板中的当前语言,以根据该语言过滤Blog帖子:

For example I request the current language in template to filter the Blog posts based on the language:

class Blog(AjaxListView):
    context_object_name = "posts"
    template_name = 'cgapp/blog.html'
    page_template = 'cgapp/post_list.html'

def get_queryset(self):
    if self.request.LANGUAGE_CODE == 'en': #request value of the current language
        return News.objects.filter(language='en') #return filtered object if the current language is English
    else:
        return News.objects.filter(language='uk')

要根据用户输入过滤查询集,可以参考POST方法:

To filter the queryset based on the users input, you may refer to POST method:

from app.forms import BlogFilterForm

class Blog(LoginRequiredMixin, AjaxListView):
    context_object_name = "posts"
    template_name = 'blog/blog.html'
    page_template = 'blog/post_list.html'
    success_url = '/blog'

    def get_queryset(self): # define queryset
        queryset = Post.objects.all() # default queryset
        if self.request.method == 'POST': # check if the request method is POST
            form = BlogFilterForm(self.request.POST) # define form
            if form.is_valid(): 
                name = form.cleaned_data['name'] # retrieve data from the form
                if name:
                    queryset = queryset.filter(name=name) # filter queryset
        else:
            queryset = queryset
        return queryset 

    def get_context_data(self, **kwargs):
        context = super(Blog, self).get_context_data(**kwargs)
        context['form'] = BlogFilterForm() # define context to render the form on GET method
        return context

    def post(self, request, *args, **kwargs): # define post method
        return super(Blog, self).get(request, args, kwargs)

无尽的分页应该可以正常工作.

The endless pagination should work fine.

这篇关于使用带有django-endless-pagination的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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