Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
django-datatables-view
Advanced tools
django-datatables-view is a base view for handling server side processing for the awesome datatables 1.9.x, 1.10.x (http://datatables.net).
django-datatables-view simplifies handling of sorting, filtering and creating JSON output, as defined at: http://datatables.net/examples/server_side/
Example project that uses django-datatables-view is available at: https://bitbucket.org/pigletto/django-datatables-view-example/
pip install django-datatables-view
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:
For more advanced customisation you might want to override:
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 to execute whole logic) is get_context_data. Definitely have a look at this method!
See example below:
from django_datatables_view.base_datatable_view import BaseDatatableView
from django.utils.html import escape
class OrderListJson(BaseDatatableView):
# The model we're going to show
model = MyModel
# define the columns that will be returned
columns = ['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 data
max_display_length = 500
def render_column(self, row, column):
# We want to render user as a custom column
if column == 'user':
# escape HTML for security reasons
return escape('{0} {1}'.format(row.customer_firstname, row.customer_lastname))
else:
return super(OrderListJson, self).render_column(row, column)
def filter_queryset(self, qs):
# use parameters passed in GET request to filter queryset
# simple example:
search = self.request.GET.get('search[value]', None)
if search:
qs = qs.filter(name__istartswith=search)
# more advanced example using extra parameters
filter_customer = self.request.GET.get('customer', None)
if filter_customer:
customer_parts = filter_customer.split(' ')
qs_params = None
for part in customer_parts:
q = Q(customer_firstname__istartswith=part) | Q(customer_lastname__istartswith=part)
qs_params = qs_params | q if qs_params else q
qs = qs.filter(qs_params)
return qs
Add typical django's urlconf entry:
url(r'^my/datatable/data/$', login_required(OrderListJson.as_view()), name='order_list_json'),
Example JS:
$(document).ready(function () {
var oTable = $('.datatable').dataTable({
// ...
"processing": true,
"serverSide": true,
"ajax": "{% url 'order_list_json' %}"
});
// ...
});
from django_datatables_view.base_datatable_view import BaseDatatableView
from django.utils.html import escape
class OrderListJson(BaseDatatableView):
order_columns = ['number', 'user', 'state']
def get_initial_queryset(self):
# return queryset used as base for further 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.
return MyModel.objects.filter(something=self.kwargs['something'])
def filter_queryset(self, qs):
# use request parameters to filter queryset
# simple example:
search = self.request.GET.get('search[value]', None)
if search:
qs = qs.filter(name__istartswith=search)
# more advanced example
filter_customer = self.request.GET.get('customer', None)
if filter_customer:
customer_parts = filter_customer.split(' ')
qs_params = None
for part in customer_parts:
q = Q(customer_firstname__istartswith=part) | Q(customer_lastname__istartswith=part)
qs_params = qs_params | q if qs_params else q
qs = qs.filter(qs_params)
return qs
def prepare_results(self, qs):
# prepare list with output column data
# queryset is already paginated here
json_data = []
for item in qs:
json_data.append([
escape(item.number), # escape HTML for security reasons
escape("{0} {1}".format(item.customer_firstname, item.customer_lastname)),
# escape HTML for security reasons
item.get_state_display(),
item.created.strftime("%Y-%m-%d %H:%M:%S"),
item.modified.strftime("%Y-%m-%d %H:%M:%S")
])
return json_data
This sample assumes that list of columns and order columns is defined on the client side (DataTables), eg.:
$(document).ready(function () {
var dt_table = $('.datatable').dataTable({
order: [[0, "desc"]],
columns: [
{
data: 'name',
orderable: true,
searchable: true
},
{
data: 'description',
orderable: true,
searchable: true,
}
],
searching: true,
processing: true,
serverSide: true,
stateSave: true,
ajax: TESTMODEL_LIST_JSON_URL
});
});
class TestModelListJson(BaseDatatableView):
model = TestModel
FAQs
Django datatables view
We found that django-datatables-view demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.