Skip to main content

django-tables2 with AJAX searching and pagination.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-responsive-tables2-0.8.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_responsive_tables2-0.8.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file django-responsive-tables2-0.8.1.tar.gz.

File metadata

File hashes

Hashes for django-responsive-tables2-0.8.1.tar.gz
Algorithm Hash digest
SHA256 2fe84b10aac7dbf4265598c976e1b3b314b72ba1e5ef8eabc83aa341812d9767
MD5 331b3c4d36f6f9f6dbaeb7a182334d7b
BLAKE2b-256 ec059c7b8081115b1f91a7f0ca64049c5298522488193cebdd7efa8195c58855

See more details on using hashes here.

File details

Details for the file django_responsive_tables2-0.8.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_responsive_tables2-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6c3e4caf861384134b67bb6c0b23a7c1efe8539f8db041b7e285c40d09dbabae
MD5 9b4ea355289aa075cee5fbf3c72ed946
BLAKE2b-256 7469f6a48eafafe4d71a7473d8851ee1647b2c528c4bf6e3cfb871f90990d894

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page