0

这是我的 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 %}",
    });
...
4

1 回答 1

1

好的,因此{% url view_name param1 param2 ... %}您将参数(param1,param2,...)传递给您的视图。view_name 是您在urlfrom中为视图定义的名称urlpatterns

因此,您必须使用此网址:

url(r'^data/<product_type>/$', DataTable.as_view(), name='DataTable'),

然后,要在你的 中捕获它product_typeDataTable你必须在其中实现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 %}仅构造一个字符串,不会进行重定向

于 2014-05-29T17:47:06.703 回答