这是我的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>
...
编辑:
views.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 %}",
});
...
答案 0 :(得分:1)
好的,所以使用{% url view_name param1 param2 ... %}
,您将参数(param1,param2,...)传递给您的视图。 view_name是您在url
urlpatterns
中为视图定义的名称。
因此,您必须使用此网址:
url(r'^data/<product_type>/$', DataTable.as_view(), name='DataTable'),
然后,要在product_type
中抓住此DataTable
,您必须在其中实施dispatch
方法:
def dispatch(self, request, *args, **kwargs):
self.product_type= kwargs.pop("product_type")
return super(LanguageMixin, self).dispatch(request, *args, **kwargs)
编辑:另一种方法是让你拥有url
并使用GET
如果你想把它作为GET参数传递,那么抓住它的最佳位置是get_context_method
:
def get_context_data(self, **kwargs):
expand_text = self.request.GET.get('product_type')
为了将其作为GET
参数捕获,您必须正确构造URL,附加GET参数。像这样:
<a href="{% url view_name %}?product_type={{ some_product_type }}">
请注意,{% url view_name %}
仅构造字符串,不进行重定向