这是我的 urls.py
from django.conf.urls import patterns, url, include
from geartables import views
from geartables.views import DataTable
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^data/$', DataTable.as_view(), name='DataTable'),
url(r'^(?P<product_type>[\w\-]+)/$', views.table, name='table'),
)
当用户转到“localhost/table/”时,会从模板生成一个页面。该模板中有一个 {% url %} 标记,它调用 DataTable 类(以创建 JSON 数据)。
所以我的问题是如何让views.py 中的DataTable 类调用我的变量?
像这样:
class DataTable(BaseDatatableView):
model = <product_type>
...
编辑:
视图.py:
...
def table(request, product_type):
ptype = ProductType.objects.get(url_name=product_type)
datatable = 'DataTable'
context = {'mselectmenus': ptype.mselects, 'columns': ptype.columnlist, 'datatable':'DataTable}
return render(request, 'geartables/dtcode.html', context)
class DataTable(BaseDatatableView):
p = ProductType.objects.get(name=<product_type>)
model = p.modelname
columnlist = p.columnlist # ex: [["th_pic","Picture"],["brand_name", "Brand"]]
mselects = p.mselects
ranges = p.ranges
max_display_length = 2000
def __init__(self):
self.columns = [item[0] for item in self.columnlist]
self.order_columns = self.columns
#multiple select filters
def mselectmenus(p):
mselectmenu = []
for item in p.mselects:
mselectmenu.append(
[item[1], item[0], p.model.objects.values_list(item[0], flat=True).order_by(item[0]).distinct()])
return mselectmenu
#initial render
def render_column(self, row, column):
if column == 'th_pic':
return '<a href=' + row.lg_pic + '><img src=' + row.th_pic + ' /></a>'
elif column == 'description':
return ''
else:
return super(DataTable, self).render_column(row, column)
#filtering
def filter_queryset(self, qs):
#search bar
ssearch = self.request.GET.get('sSearch', None)
if ssearch:
qs = qs.filter(Q(brand_name__icontains=ssearch) | Q(product_name__icontains=ssearch) | Q(
rope_type__icontains=ssearch) | Q(length__icontains=ssearch) | Q(diameter__icontains=ssearch) | Q(
weight__icontains=ssearch) | Q(falls__icontains=ssearch) | Q(color__icontains=ssearch))
#mselect
ssearch_1 = self.request.GET.get('sSearch_1', None)
if ssearch_1:
qs = qs.filter(Q(brand_name__regex=ssearch_1))
return qs
...
模板代码:
...
$(document).ready(function() {
var dtable = $('#{{ ptable }}').dataTable( {
//"oLanguage": oLanguages,
"sDom": 'C<"clear">lfrtip',
"aoColumns": [
{ "bSortable": false, "bSearchable:": false, "sClass": "center" },
{ "bSortable": true, "bSearchable:": true, "sClass": "center" },
{ "bSortable": true, "bSearchable:": true, "sClass": "center" },
{ "bSortable": true, "bSearchable:": false, "sClass": "center" },
{ "bSortable": true, "bSearchable:": true, "sClass": "center" },
{ "bSortable": true, "bSearchable:": true, "sClass": "center" },
{ "bSortable": true, "bSearchable:": true, "sClass": "center" },
{ "bSortable": true, "bSearchable:": true, "sClass": "center" },
{ "bSortable": true, "bSearchable:": true, "sClass": "center" },
{ "bSortable": true, "bSearchable:": false, "sClass": "center" },
],
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "{% url datatable %}",
});
...