A Django package that provides an easy way to optimize JQuery Datatables in Django through Serverside Rendering.
Project description
This package provides an easy way to optimize JQuery Datatables in Django through Serverside Rendering.
All you have to do is to create a new view, configure the model to be used and the columns to be rendered, and you’re all set!
Supported features are pagination, column ordering, row value customization, and global search (not restricted to a specific column). The searching function can find values in any string-convertible field, and also searched with choice descriptions of predefined choices fields.
Foreign key fields can be used, provided that a QuerySet-like access path (i.e. model2__field) is given in the configuration.
Quick start
Create a view that inherits the `DatatablesServerSideView` mixin:
from datatables_optimization.mixins import DatatablesServerSideView from django.contrib.humanize.templatetags.humanize import intcomma # For demo purposes class ApplicationsListServerSideView(DatatablesServerSideView): """ View will render serverside processing for the **Application** model. """ model = Application columns = ['full_name', 'admission_number', 'category', 'amount'] searchable_columns = ['full_name', 'admission_number'] foreign_fields = {'category': 'category__name'} def customize_row(self, row, obj): # Here we customize the amount row['awarded_amount'] = intcomma(obj.amount) def get_initial_queryset(self): qs = super(ApplicationsListServerSideView, self).get_initial_queryset() # Customize Queryset as deemed appropriate return qs
Add a `URL Path` that points to our view:
urlpatterns = [ # Other paths... path('applications/serverside/', ApplicationsListServerSideView.as_view(), name='applications-list-serverside'), ]
Add the Serverside processing in your DataTables template:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap4.min.css"> </head> <body> <h1>List of Application</h1> <hr> <table id="table"> <thead> <tr> <th>Full Name</th> <th>Adm. Number</th> <th>Category</th> <th>Amount</th> </tr> </thead> <tbody></tbody> </table> <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script> <sscript src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script> <script> $(document).ready(function () { /* Here begins the DataTable configuration. */ $('#table').DataTable({ /* Tell the DataTable that we need server-side processing. */ serverSide: true, /* Set up the data source */ ajax: { url: "{% url "applications-list-serverside" %}" }, /* And set up the columns. Note that they must be identified by a "name" attribute, with the value matching the columns in your Django view. The "data" attribute selects which record value will be used, and should be the same value than for the "name" attribute. */ columns: [ {name: "full_name", data: "full_name"}, {name: "admission_number", data: "admission_number"}, {name: "category", data: "category"}, {name: "amount", data: "amount"}, ] }); }); </script> </body> </html>
The view will return a `HTTPResponseBadRequest` if the request is not an `AJAX` request, or if params seem to be malformed. Else you should have your datatable serverside processing working just fine.
Happy Development!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django-datatables-optimization-1.0.tar.gz
.
File metadata
- Download URL: django-datatables-optimization-1.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.1+
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc43428156a8bc9e63a161b1e99f850a90240087dd190578ad859d5e8e2f2129 |
|
MD5 | 60fa45a33e19f1314d9ac6e1f67f4fa9 |
|
BLAKE2b-256 | 913c65e73f8f76cb3cacab078fbdf56853db46068f23cc33acec9c3f49e7a8f6 |