Django分页符破坏了我的order_by [英] Django-pagination breaks my order_by

查看:77
本文介绍了Django分页符破坏了我的order_by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的html模板中,我加载了一个对象列表,现在我试图将Django-Pagination(来自文档)和order_by方法结合起来.

In my html-template I load a list of objects and I'm now trying to combine Django-Pagination (from the docs) and the order_by method.

我试图放置一些排序链接,以加载href ="/mylist/?order_by = somefield

I tried to put some ordering-links which loads the href="/mylist/?order_by=somefield

它在第一页上起作用.但是,当我单击下一页"时,顺序似乎中断了.这是什么问题?

It works on the first page. But the order seems to break when I'm clicking the "next page". What's the problem here?

下一页"链接加载href ="mylist/?page = {{results.next_page_number}}

"Next page" link loads the href="mylist/?page={{ results.next_page_number }}

查看:

    def Mylist(request):

        order_by = request.GET.get('order_by', 'somedefault')   
        myobjects_list = Mymodel.objects.filter(user=request.user).order_by(order_by)
        paginator = Paginator(myobjects_list, 5)

        page = request.GET.get('page')
        try:
            results = paginator.page(page)
        except PageNotAnInteger:
            results = paginator.page(1)
        except EmptyPage:
            results = paginator.page(paginator.num_pages)

        context = {'results ': results }
        return render_to_response('mylist.html', context, context_instance=RequestContext(request))

推荐答案

视图中:

context = {'results ': results, 'order_by': order_by}

在模板中:

href="mylist/?page={{ results.next_page_number }}&order_by={{ order_by }}"

这篇关于Django分页符破坏了我的order_by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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