Here is my 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'),
)
When the user goes to "localhost/table/", a page is generated from a template. Within that template is a {% url %} tag that calls the DataTable class (to create JSON data).
So my question is how can I get the DataTable class in views.py to call my variable?
Like so:
class DataTable(BaseDatatableView):
model = <product_type>
...
edit:
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
...
template code:
...
$(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 %}",
});
...
Ok, so with {% url view_name param1 param2 ... %}
you are passing arguments(param1, param2,...) to your view. view_name is the name you defined for your view in the url
from urlpatterns
.
Therefore, you have to use this url:
url(r'^data/<product_type>/$', DataTable.as_view(), name='DataTable'),
Then, to catch this product_type
in your DataTable
, you have to implement the dispatch
method inside it:
def dispatch(self, request, *args, **kwargs):
self.product_type= kwargs.pop("product_type")
return super(LanguageMixin, self).dispatch(request, *args, **kwargs)
EDIT: Another way is to let the url
as you have and use GET
If you want to pass it as a GET parameter, then the best place to catch it is inside the get_context_method
:
def get_context_data(self, **kwargs):
expand_text = self.request.GET.get('product_type')
In order to catch it as a GET
parameter, you have to construct the URL properly, appending the GET parameters. Something like this:
<a href="{% url view_name %}?product_type={{ some_product_type }}">
Keep in mind that {% url view_name %}
only constructs a string, does not make a redirect