Skip to main content

django_responsive_tables2

This project is intended to provide added functionality to the already outstanding django_tables2 library. Using this package, you'll be able to easily implement responsive full-text search capabilities, pagination and more.

Installation

This can be installed using pip by following the steps below.

In your Python environment, install django-responsive-tables2.

pip install django-responsive-tables2

Add the following to your projects INSTALLED_APPS variable found in settings.py so it looks as such.

INSTALLED_APPS = [
    'django_tables2',
    'django_responsive_tables2',
]

From there, we can begin by adding a table class in a new file named tables.py located in your apps directory, as we would with django_tables2.

class PersonTable(tables.Table):
    class Meta:
        model = Person
        template_name = "django_tables2/bootstrap.html"
        fields = ("name", )

Once we've made our table class, we can add a function in our apps views.py file to handle AJAX requests for table data. You will find below a function named build_filter, however this mainly intended for development purposes, and a more efficient full-text search algorithm should be used for production.

def build_filter(search, *args, **kwargs):
    """
    Builds a search filter given the search parameters & search text.
    Use like a regular filter, except set any values you want searched set to True.
    """
    children = [*args, *sorted(kwargs.items())]
    search_keywords = search.split()
    search_filter = Q(_connector="AND")
    for keyword in search_keywords:
        current_filter = Q(_connector="OR")
        for (key, value) in children:
            if value is True:
                current_filter.children.append((key, keyword))
        search_filter.children.append(current_filter)
    return search_filter


def table_person(request):
    # Get the search query, if it exists.
    search = request.GET.get("search")
    # Check to see if the search query exists.
    if search:
        # Search for specified fields, etc.
        search_filter = build_filter(search, first_name__icontains=True, last_name__icontains=True)
        table = PersonTable(Person.objects.filter(search_filter))
    else:
        table = PersonTable(Person.objects.all())

    RequestConfig(request).configure(table)
    return HttpResponse(table.as_html(request))

From there, we must add a URL pattern so the table can fetch the tables contents, and update the table contents when something is searched, or a page is changed. In our apps urls.py file, we can add the following URL routing.

path('table-person/', views.table_person, name="TablePerson")

Now that we have our table requests, search handling & URL routing finished, we can begin to implement it into one of our views templates. There is nothing needed in the context of the view you want the table on, the only thing that is required for having the table appear on your page is to have it implemented into your views template.

Inside the template of the view you'd like the table to appear we must load static files and the responsive_table template tag.

{% load static %}
{% load responsive_table from responsive_table %}

Now add the following script to the bottom of the body tag, or to the head tag.

<script src="{% static 'django_responsive_tables2/tables.js' %}" type="application/javascript"></script>

Then, wherever you would like the table to show on your template, insert the table as such. The first argument is the HTML ID of the table & related objects. This can be anything you'd like however if there are multiple tables on your page these must be different. The second argument is the URL pattern we created for handling the AJAX requests.

{% responsive_table "myTableID" "example:TablePerson" %}

All done! From there we can add as many more tables to our template as we please.

Metadata

Release files for django-responsive-tables2 0.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-responsive-tables2 0.8.1
File Size Uploaded
django-responsive-tables2-0.8.1.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-responsive-tables2 0.8.1
File Interpreter ABI Platform
django_responsive_tables2-0.8.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.0 kB

Release files / django-responsive-tables2-0.8.1.tar.gz

Download URL django-responsive-tables2-0.8.1.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2fe84b10aac7dbf4265598c976e1b3b314b72ba1e5ef8eabc83aa341812d9767
BLAKE2b-256 checksum
How to use checksums
ec059c7b8081115b1f91a7f0ca64049c5298522488193cebdd7efa8195c58855
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.1

Release files / django_responsive_tables2-0.8.1-py3-none-any.whl

Download URL django_responsive_tables2-0.8.1-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6c3e4caf861384134b67bb6c0b23a7c1efe8539f8db041b7e285c40d09dbabae
BLAKE2b-256 checksum
How to use checksums
7469f6a48eafafe4d71a7473d8851ee1647b2c528c4bf6e3cfb871f90990d894
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.1

Release history Release notifications | RSS feed

This release

0.8.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page