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

刚学习Django写了一个搜索的视图函数,想对搜索内容进行分页,现在有个问题,搜索内容提交后可以正常分页并显示第一页,选择第二个页时由于GET内容没传过去页面就显示不出来... 刚学习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">
Page{{results.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={{}}中,但我不知道这里面放什么,希望朋友您能教一下,十分感谢
展开
 我来答
lpe234
2015-02-04
知道答主
回答量:0
采纳率:0%
帮助的人:0
展开全部

很简单的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 %}
来自:求助得到的回答
匿名用户
推荐于2018-04-15
展开全部

给你个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
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
?>

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式