添加"搜索"同时保持分页和排序?

时间:2015-05-13 15:44:18

标签: python django django-tables2

我想添加"搜索"我的模型的功能,但[querystring][1]似乎没有像我预期的那样工作。这是我的尝试:

from django.db import models

class ProductsByOneDayMax(models.Model):
    product = models.TextField(max_length=65535, verbose_name="Product name")
    max = models.IntegerField(verbose_name="Max daily IPs")

    class Meta:
        db_table = 'precomputed_product_distinct_ip_one_day_max'

from django.db import connection as conn
from django.shortcuts import render
from viewer.models import ProductsByOneDayMax
import django_tables2 as tables

def list_products(request):
    class ProductsByOneDayMaxTable(tables.Table):
        class Meta:
            model = ProductsByOneDayMax
            exclude = ('id', )
    search = request.GET.get('search', '')
    objects = ProductsByOneDayMax.objects.filter(product__icontains=search)
    table = ProductsByOneDayMaxTable(objects)
    table.order_by = "-max"
    tables.RequestConfig(request).configure(table)
    return render(request, "plain_table.html", {'table': table,
                                                'title': 'Product list',
                                                'search': search})

观点:

{% extends "base.html" %}
{% block content %}
{% load django_tables2 %}
{% querystring "search"=search %}
 <form class="form-inline" method="get" role="form">
        <div class="input-group">
            <span class="input-group-addon"><span class="glyphicon glyphicon-search"></span></span>
            <input type="text" class="form-control " value="" placeholder="Search..." name="search">
        </div>

        <button type="submit" class="btn btn-primary">Search</button>
</form>
{% render_table table %}
{% endblock %}

而不是添加&#34;搜索&#34;字段到查询字符串,这只会添加到输出中。我做错了什么?

如果相关,我使用的是bootstrap-tables2.css

1 个答案:

答案 0 :(得分:1)

看起来这是在链接模板的最后一个GitHub评论中解决的:

  

bootstrap_pagination标记需要完整的URL才能正常使用   对页面之间的列进行排序:

{% bootstrap_pagination table.page url=request.get_full_path %}
     

此   假设你有"django.core.context_processors.request"   settings.TEMPLATE_CONTEXT_PROCESSORS

修改模板解决了问题。