I have implemented Django tables2 with pagination, 2000 pages 25 records per page. There are 2 things I'm trying to do:

  1. Add a filter
  2. Add a custom column to the table

Detail:

  1. I've installed Django-filters, but I'm having trouble using it within tables2, I show the code from the docs in the filter.py, I already have bootstrap css implemented, but I only want to filter on the table columns, preferably with the ability to customise the layout of the filter form.

  2. I need to add a column which contains a link to a function, a simple url - details not important here: <td><a href="{% url 'depot:change_container' operation='add' pk=container.container_id fk=contents.sample_id %}" class="badge badge-primary" role="button">add</a></td>. So how can I add an extra column to the table?

My code in summary to the relevent parts

# tables.py
import django_tables2 as tables
from .models import Sample

class SampleTable(tables.Table):
    class Meta:
        model = Sample

# views.py
def detailcontainer(request, container_id):
    unassigned_samples2 = Sample.objects.all()
    table = SampleTable(unassigned_samples2)
    RequestConfig(request, paginate={'per_page': 25}).configure(table)

    return render(request, 'container/detailcontainer.html',
    {'table':table})

# filters.py
from django_filters.views import FilterView
from django_tables2.views import SingleTableMixin

class FilterSample(SingleTableMixin, FilterView):
    table_class = SampleTable
    model = Sample

    filterset_class = SampleFilter


# template # this is what the docs say, but I can't get the 'if filter' section to work
  {% load render_table from django_tables2 %}
  {% render_table table %}

  {% if filter %}
    <form action="" method="get" class="form form-inline">
        {% bootstrap_form filter.form layout='inline' %}
        {% bootstrap_button 'filter' %}
    </form>
  {% endif %}

0 Answers