Reputation: 11561
I wanted to add the "search" functionality to my model, but the [querystring][1]
doesn't seem to work as I expected it to. Here's my attempt:
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})
And the view:
{% 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 %}
Instead of adding the "search" field to the querystring, this only gets added to the output. What am I doing wrong?
In case it's relevant, I am using bootstrap-tables2.css.
Upvotes: 1
Views: 264
Reputation: 11561
Looks like this was solved in the last GitHub comment for the linked template:
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
Modifying the template solved the problem.
Upvotes: 1