Package: classy-django-datatables
Django DatatablesThe django datatables library makes creating tables that make use of the datatables library simple, reusable, pythonic, djangoesque, and quite a bit fun.Project GoalsAllow creation of tables in a style similar to django forms.Remove tedious editing of datatables javascript config to match columns.Configure ajax URLs automagically.Simplify use of Django style URLs within the datatableInstallationpip install classy-django-datatablesQuick SetupDownload the library and place it somewhere accessable in your PYTHONPATH.  The following is a basic example to demonstrate the ease to get up and running.settings.pyAdddjango_datablesto theINSTALLED_APPSsetting.INSTALLED_APPS=[...'django_datatables',...]urls.pyAdd the following line to urls.py.url(r'^__django_datatables__/',include('django_datatables.urls')),views.pyfromdjango_datatables.datatableimport*fromdjango_datatablesimportcolumnclassStudyListDatatable(Datatable):code_name=column.TextColumn()classMeta:model=Studydefstudy_list(request)datatable=StudyListDatatable()returnrender(request,'datatables_demo.html',{"datatable":datatable})Template{{datatable.render}}Building UpCustom Title/ValueIn the example shown, the code_name, as the variable name is automatically used to fetch the field and then used as the header for the column.  There will often be cases where the variable name will not coincide to either of these and can be overritten with the following:created_date=column.TextColumn(title='Made on')scientist=column.TextColumn(value='scientist__scientist_name')CSS ClassA css class to apply to each cell in the column.scientist=column.TextColumn(css_class='text-danger')Joined tablesFields in joined tables are accessed using the same syntax used in django.scientist=column.TextColumn(value='scientist__scientist_name')Adding linksLinks support django's URL dispatcher.  Just add the URL name to the link attribute and the arguments that get passed to the link.  You don't even need the column listed -- Django Datables will integentally fetch the needed field and populate the links accordingly.code_name=column.TextColumn(link='edit_study',link_args=['slug'])Data from multiple fieldsTo pull data from multiple fields into one column, declare the column as aStringColumn.  If needed, add the fields to be requested from the database in theMeta.extra_fieldsattribute.  Finally, render the desired with the render_* method.name=column.StringColumn()classMeta:model=Employeeextra_fields=('first_name','last_name')defrender_name(self,row):return"{}{}".format(row['first_name'],row['last_name']).strip()Other QuerysetsThe initial queryset can be overridden if a more complex query is needed, or if a default filter needs to be in place.defget_initial_queryset(self,request):returnEmployee.objects.filter(manager=request.user)Metamodel: the primary model to be displayed in the tablemodel=Studyorder_columns: a list of the columns that can be sortedorder_columns=['study_name','created_date','modified_date','scientist']initial_order: the inital sort of the tableinitial_order=['created_date','scientist']searching: (default:false) Enable the search boxsearch_fieldsthe fields that the search box will search for content.  This can be more finely controlled in the filter_by_search() method.search_fields=['study_name','code_name','scientist__scientist_name']title: The title of the report.  Only used for the filename and sheet name of the excel export.title="Study List"export_to_excel: If openpyxl is installed and set to true, will display a link to download an excel file containing all rows in the table.export_to_excel=TrueCustom renderingAny field can have it's render method extended using render_*defrender_code_name(self,value):returnvalue.lower()defrender_created_date(self,value):returnvalue.strftime("%m/%d/%Y")ColumnsAttributestitle - Displayed in the headercss_class - A CSS class to apply to the columnvalue - The value in the database to uselink - The django url name this column will link tolink_args - the link argumentsThe following column types are available in the django_datatables.column module.TextColumn: A standard column that will display the contents of a single field.ConstantTextColumn(text): Will display text independant of the database.  Ex: Edit, or DeleteStringColumn: A column that will render text using multiple fields.  Request the data withMeta.extra_fieldsand format the text with the render_* method.CheckBoxColumn: Render a checkbox.GlyphiconColumn(icon): Displayan icon from bootstrap's v3 glyphicon set.FontAwesome4(icon): Display an icon from the Font Awesome 4 library. Ex:column.FontAwesome4Column('stop-circle fa-2x')(Must manually include bootstrap in source.)DateColumn: Render a date in Y-m-d format.FiltersFilter forms can be connected to the datatable by assigning a django form to Meta.filter_form.  Naming the fields as django queryset keys (eg:name__icontains,count__gte) will auto filter the form as needed.Filter exampleclassEmployeeFilterForm(forms.Form):last_name__icontains=forms.CharField(label="Last Name",required=False)classStudyListDatatable(datatable.Datatable):name=column.StringColumn()classMeta:model=Employeefilter_form=EmployeeFilterFormextra_fields=('first_name','last_name')defrender_name(self,row):return"{}{}".format(row['first_name'],row['last_name']).strip()In the template, the form can be displayed with the following.  There /must/ be a.datatable-formclass attached to the form.<formclass='datatable-form'>{{datatable.filter_form}}<buttontype="submit">Submit</button></form>PermissionsIt's important to not just lock down the view, but also the ajax call that retrieves the data.  Fortunately, authentication is easily handled with mixins.  Django 1.9 ships with LoginRequiredMixin, UserPassesTestMixin, and PermissionRequiredMixin which handle most use cases.  Ensure the permission-related mixins are stated first.More information regarding mixins can be found at the officialdjango authentication doc.fromdjango.contrib.auth.mixinsimportLoginRequiredMixinclassEmployeeListDatatable(LoginRequiredMixin,datatable.Datatable):...