Example

Usage

1. Install django-datatables-view

pip install django-datatables-view

2. Edit views.py

django_datatables_view uses GenericViews, so your view should just inherit from base class: BaseDatatableView, and override few things
(there is also a DatatableMixin - pure datatables handler that can be used with the mixins of your choice, eg. django-braces)
These are:

model - the model that should be used to populate the datatable

columns - the columns that are going to be displayed

order_columns - list of column names used for sorting (eg. if user sorts by second column then second column name from this list will be used with order by clause).

filter_queryset - if you want to filter your datatable then override this method

For more advanced customisation you might want to override:

get_initial_queryset - method that should return queryset used to populate datatable

prepare_results - this method should return list of lists (rows with columns) as needed by datatables

The code is rather simple so do not hesitate to have a look at it. Method that is executed first (and that calls other methods) is get_context_data

See example below:

fromdjango_datatables_view.base_datatable_viewimportBaseDatatableViewclassOrderListJson(BaseDatatableView):# The model we're going to showmodel=MyModel# define the columns that will be returnedcolumns=['number','user','state','created','modified']# define column names that will be used in sorting# order is important and should be same as order of columns# displayed by datatables. For non sortable columns use empty# value like ''order_columns=['number','user','state','','']# set max limit of records returned, this is used to protect our site if someone tries to attack our site# and make it return huge amount of datamax_display_length=500defrender_column(self,row,column):# We want to render user as a custom columnifcolumn=='user':return'{0} {1}'.format(row.customer_firstname,row.customer_lastname)else:returnsuper(OrderListJson,self).render_column(row,column)deffilter_queryset(self,qs):# use parameters passed in GET request to filter queryset# simple example:search=self.request.GET.get(u'search[value]',None)ifsearch:qs=qs.filter(name__istartswith=search)# more advanced example using extra parametersfilter_customer=self.request.GET.get(u'customer',None)iffilter_customer:customer_parts=filter_customer.split(' ')qs_params=Noneforpartincustomer_parts:q=Q(customer_firstname__istartswith=part)|Q(customer_lastname__istartswith=part)qs_params=qs_params|qifqs_paramselseqqs=qs.filter(qs_params)returnqs

4. Define HTML + JavaScript

Another example of views.py customisation

fromdjango_datatables_view.base_datatable_viewimportBaseDatatableViewclassOrderListJson(BaseDatatableView):order_columns=['number','user','state']defget_initial_queryset(self):# return queryset used as base for futher sorting/filtering# these are simply objects displayed in datatable# You should not filter data returned here by any filter values entered by user. This is because# we need some base queryset to count total number of records.returnMyModel.objects.filter(something=self.kwargs['something'])deffilter_queryset(self,qs):# use request parameters to filter queryset# simple example:search=self.request.GET.get(u'search[value]',None)ifsearch:qs=qs.filter(name__istartswith=search)# more advanced examplefilter_customer=self.request.GET.get(u'customer',None)iffilter_customer:customer_parts=filter_customer.split(' ')qs_params=Noneforpartincustomer_parts:q=Q(customer_firstname__istartswith=part)|Q(customer_lastname__istartswith=part)qs_params=qs_params|qifqs_paramselseqqs=qs.filter(qs_params)returnqsdefprepare_results(self,qs):# prepare list with output column data# queryset is already paginated herejson_data=[]foriteminqs:json_data.append([item.number,"{0} {1}".format(item.customer_firstname,item.customer_lastname),item.get_state_display(),item.created.strftime("%Y-%m-%d %H:%M:%S"),item.modified.strftime("%Y-%m-%d %H:%M:%S")])returnjson_data