Skip to main content

Django integration with jQuery DataTables.

Project description

Django DataTables Too

Author:Tim Santor tsantor@xstudios.agency

Overview

Handle server side processing for datatables 1.10.x.

Getting It

To install Django DataTables Too, just use pip:

$ pip install django-datatables-too

Usage

views.py

from django.http import JsonResponse
from django.views.generic import View
from django_datatables_too.mixins import DataTableMixin

class DataTablesAjaxPagination(DataTableMixin, View):
    model = Report
    queryset = Report.objects.all()

    def _get_actions(self, obj):
        """Get action buttons w/links."""
        return f'<a href="{obj.get_update_url()}" title="Edit" class="btn btn-primary btn-xs"><i class="fa fa-pencil"></i></a> <a data-title="{obj}" title="Delete" href="{obj.get_delete_url()}" class="btn btn-danger btn-xs btn-delete"><i class="fa fa-trash"></i></a>'

    def filter_queryset(self, qs):
        """Return the list of items for this view."""
        # If a search term, filter the query
        if self.search:
            return qs.filter(
                Q(number__icontains=self.search) |
                Q(title__icontains=self.search) |
                Q(state__icontains=self.search) |
                Q(year__icontains=self.search)
            )
        return qs

    def prepare_results(self, qs):
        # Create row data for datatables
        data = []
        for o in qs:
            data.append({
                'number': o.number,
                'title': Truncator(o.title).words(10),
                'state': o.state,
                'year': o.year,
                'published': o.published,
                'modified': o.modified,
                'actions': self._get_actions(o)
            })
        return data

    def get(self, request, *args, **kwargs):
        context_data = self.get_context_data(request)
        return JsonResponse(context_data)

urls.py

from django.urls import path

from . import views

app_name = 'reports'

urlpatterns = [

    ...

    path('ajax',
        views.DataTablesAjaxPagination.as_view(), name='report-list-ajax'),

]

report_list.html

$('#report-table').DataTable({
    columnDefs: [{
        orderable: true,
        targets: -1
    }, ],

    // Ajax for pagination
    processing: true,
    serverSide: true,
    ajax: {
        url: '{% url "reports:report-list-ajax" %}',
        type: 'get',
    },
    columns: [
        // data: json key from prepare_results, name: model field name
        { data: 'number', name: 'number'},
        { data: 'title', name: 'title' },
        { data: 'state', name: 'state' },
        { data: 'year', name: 'year' },
        { data: 'published', name: 'published' },
        { data: 'modified', name: 'modified' },
        { data: 'actions', name: 'actions' }
    ]

});

Local Development

make env
make pip_install
make migrations
make migrate
make superuser
make serve

or simply make from_scratch

  • Visit http://127.0.0.1:8000/admin/ for the Django Admin

Testing

make pytest
make coverage
make open_coverage

0.1.5 2023-01-13

  • Added Cache-Control: no-cache header

0.1.4 2021-08-30

  • Fixed sorting now actually works

0.1.3 2019‑08‑16

  • Fixed fixed issue where HISTORY.rst was not included in the package causing it to fail upon install

0.1.2 2019‑08‑16

  • Fixed bug when DataTable was set to ordering: false in JavaScript

0.1.1 2018‑09‑25

  • Initial release

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_datatables_too-0.1.6.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

django_datatables_too-0.1.6-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file django_datatables_too-0.1.6.tar.gz.

File metadata

  • Download URL: django_datatables_too-0.1.6.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.11

File hashes

Hashes for django_datatables_too-0.1.6.tar.gz
Algorithm Hash digest
SHA256 4d7f8866b427a81794e742b8d10f5b9649242658e7bc48cec15dc518f2f8a3f9
MD5 777ffebb7d2082a5d191d1b3c3125be2
BLAKE2b-256 ab84404e95cca972df8f1dd1ed879204050ab52d6e7318a4d4d1fd5b05eec867

See more details on using hashes here.

File details

Details for the file django_datatables_too-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for django_datatables_too-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 304b0ea810e32de92fc1de523c3f03c144fa56a6312e3c5f6e7e711dcc449327
MD5 11e6ca89e5aad235261ad0bc22ad4952
BLAKE2b-256 3ae542a9bcdc63ed696de1a8531f0c7cc22f9a3755da770f7d60843b130decc5

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