朋友,您好,我想问Django分页后查询条件丢失的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了朋友,您好,我想问Django分页后查询条件丢失的问题相关的知识,希望对你有一定的参考价值。

刚学习Django写了一个搜索的视图函数,想对搜索内容进行分页,现在有个问题,搜索内容提交后可以正常分页并显示第一页,选择第二个页时由于GET内容没传过去页面就显示不出来,请问哪个大神有好的解决办法 这是我的控件部分:查询部分
<form action="." method="GET"> <fieldset id="search_fieldset" style="paddng:20px;background:#3CF; border:#ccc 1px solid"> <input type="text" name="q" value="query | escape" class="input_text"> <input type="submit" value="Search" class="search_button"> </fieldset> </form>
分页部分
<div class = "pagination">
<span class="step-links">
% if results.has_previous %
# 这里需要将原来的条件传递过来 #
<a href="?page=results.previous_page_number&q=">previous</a>
% endif %
<span class = "current">
Pageresults.number of results.paginator.num_pages
</span>
% if results.has_next %
<a href="?page=apps.next_page_number&q=">next</a>
% endif %
</span>
</div>
网上有人说需要把查询条件放在 <a href="?page=results.previous_page_number&q=">previous</a>的&q=中,但我不知道这里面放什么,希望朋友您能教一下,十分感谢

很简单的GET传值,晚上再回答吧,现在有点事...


给你个示例代码吧

# views.py

def order_forms_by_days(request):
    """
     订单日统计报表,读取 本地 count_days 表
    :param request:
    :return:
    """
    cursor = connections['mfashion_db'].cursor()
    sql = 'SELECT * FROM count_days ORDER BY date DESC'
    cursor.execute(sql)
    results = dictfetchall(cursor)

    limit = 20
    paginator = Paginator(results, limit)
    page = request.GET.get('page')
    try:
        orders = paginator.page(page)
    except(PageNotAnInteger, EmptyPage):
        orders = paginator.page(1)

    params = 
        'request': request,
        'orders': orders,
        'page': page
    

    return render_to_response('boss/census_days.html', params, context_instance=RequestContext(request))

% with orders as datas %
        <div class="ui page grid floated" style="margin-top: 30px;">
            <div class="column">
                <h2>订单日统计报表</h2>
                订单总数量: <h3> all_counts </h3>总金额(元): <h3> all_price </h3>
                <table class="ui compact small striped table">
                    <thead>
                    <tr>
                        <th>订单编号</th>
                        <th>下单时间</th>
                        <th>支付时间</th>
                        <th>买家姓名</th>
                        <th>卖家名称</th>
                        <th>订单状态</th>
                        <th>商品名称</th>
                        <th>订单金额</th>
                    </tr>
                    </thead>
                    <tbody>
                    % for order in orders %
                        <tr>
                            <td> order.trade_no </td>
                            <td> order.create_time </td>
                            <td> order.paid </td>
                            <td> order.shipped </td>
                            <td> order.sign </td>
                            <td> order.completed </td>
                            <td> order.cancelorder </td>
                            <td> order.deliveryfailure </td>
                            <td> order.refund </td>
                            <td> order.refuseorders </td>
                            <td> order.closeorders </td>
                            <td> order.total </td>
                        </tr>
                    % endfor %
                    </tbody>
                </table>
                <div class="ui two column centered grid">
                    <div class="column centered row">
                        <div class="ui pagination menu">

                            % if datas.has_previous %
                                <a class="icon item"
                                   href="?% url_replace request 'page' datas.previous_page_number %">
                                    <i class="left arrow icon"></i>
                                </a>
                                % else %<a class="disabled icon item"><i class="left arrow icon"></i>
                                </a>
                            % endif %
                            <div class="item">
                                 datas.number / datas.paginator.num_pages 
                            </div>
                            % if datas.has_next %
                                <a class="icon item"
                                   href="?% url_replace request 'page' datas.next_page_number %">
                                    <i class="right arrow icon"></i>
                                </a>
                                % else %<a class="disabled icon item"><i class="right arrow icon"></i>
                                </a>
                            % endif %
                        </div>
                    </div>
                </div>
            </div>
        </div>

    % endwith %

来自:求助得到的回答
参考技术A

给你个Pager类的源代码吧:

class Pager:
    def __init__(self,size,offset,request):
        self.offset = offset;
        self.path = request.path;
        self.query_dict = request.GET.copy()
        self.size = size;
    def has_previous(self):
        if self.offset>0:
            return True
        else:
            return False
    def has_next(self):
        if self.size>20:
            return True
        else:
            return False
    def previous_url(self):
        if self.has_previous():
            self.query_dict["offset"]=self.offset-20
            return self.path+"?"+self.query_dict.urlencode()
        else:
            return None
    def next_url(self):
        if self.has_next():
            self.query_dict["offset"]=self.offset+20
            return self.path+"?"+self.query_dict.urlencode()
        else:
            return None

以上是关于朋友,您好,我想问Django分页后查询条件丢失的问题的主要内容,如果未能解决你的问题,请参考以下文章

react antd Form 切换Table分页后Table内Input数据丢失问题处理

django 做了搜索之后再做分页 结果显示第一页是正常的 但是按下一页后显示出整个主页的第二页

分页后的数据展示问题分析

gridview 实现分页后发现数据丢失。(第二页点了之后没数据) 求指导,谢谢了!

MybatisPlus分页条件查询

django 中的分页 - 原始查询字符串丢失