如何为FilterView添加分页

时间:2017-10-26 18:54:38

标签: django django-filter django-pagination

我已经过滤了从def isSymmetric(self, root) #single node or no node if root == null : return false else : return dfs(root) def dfs(root): #single node if(root.left == null and root.right == null): return true #both childs exist if(root.left != null and root.right != null): return (dfs(root.left) & dfs(root.right)) #Asymmetric else : return false 开始调用的django_filters.FilterSet视图

urls.py

并且它没有分页,但是当我在

中添加url(r'^$', FilterView.as_view(filterset_class=ProductFilter, template_name='products/products.html'), name='products'),
paginate_by = 20

它添加了我的自定义分页页面,但它没有处理过滤器限制的数据。所以我可以应用一些过滤器,它会将数据减少到40行,但是点击第二页,它会加载我的所有数据而不需要任何过滤器。我可以指定过滤后以某种方式对数据进行分页吗?

2 个答案:

答案 0 :(得分:2)

最后,我决定创建单独的视图,并将查询集直接添加到上下文对象,如:

class ProductView(ListView):
    model = Product
    template_name = 'products/products.html'
    paginate_by = 5

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['product_list'] = ProductFilter(self.request.GET, queryset=Product.objects.order_by('id')).qs
        return context

答案 1 :(得分:0)

我发现在给定视图上使用滤镜实现分页要简单得多:

import RealmSwift

@objc func signIn() {
    logIn(username: username!, password: password!, register: false)
}

@objc func signUp() {
    logIn(username: username!, password: password!, register: true)
}

// Log in with the username and password, optionally registering a user.
func logIn(username: String, password: String, register: Bool) {
    print("Log in as user: \(username) with register: \(register)");
    setLoading(true);
    let creds = SyncCredentials.usernamePassword(username: username, password: password, register: register);
    SyncUser.logIn(with: creds, server: Constants.AUTH_URL, onCompletion: { [weak self](user, err) in
        self!.setLoading(false);
        if let error = err { 
            // Auth error: user already exists? Try logging in as that user.
            print("Login failed: \(error)");
            self!.errorLabel.text = "Login failed: \(error.localizedDescription)"
            return;
        }
        print("Login succeeded!");
        self!.navigationController!.pushViewController(ItemsViewController(), animated: true);
    });
}