在保持分页和排序的同时添加 "search"?
Adding "search" while keeping pagination and sorting?
我想将 "search" 功能添加到我的模型中,但 [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 %}
而不是将 "search" 字段添加到查询字符串,这只会添加到输出中。我做错了什么?
如果相关,我使用 bootstrap-tables2.css。
看起来这已在链接模板的最后 GitHub 评论中解决:
The bootstrap_pagination tag needs the full URL in order to properly
sort columns between pages:
{% bootstrap_pagination table.page url=request.get_full_path %}
This
assumes you have "django.core.context_processors.request"
in
settings.TEMPLATE_CONTEXT_PROCESSORS
修改模板解决问题。
我想将 "search" 功能添加到我的模型中,但 [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 %}
而不是将 "search" 字段添加到查询字符串,这只会添加到输出中。我做错了什么?
如果相关,我使用 bootstrap-tables2.css。
看起来这已在链接模板的最后 GitHub 评论中解决:
The bootstrap_pagination tag needs the full URL in order to properly sort columns between pages:
{% bootstrap_pagination table.page url=request.get_full_path %}
This assumes you have
"django.core.context_processors.request"
insettings.TEMPLATE_CONTEXT_PROCESSORS
修改模板解决问题。