溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Datables 服務(wù)端分頁(yè)for DJango

發(fā)布時(shí)間:2020-07-24 21:58:16 來(lái)源:網(wǎng)絡(luò) 閱讀:4011 作者:skskevin 欄目:開(kāi)發(fā)技術(shù)
  1. 安裝

    django-datatables-view

    pip install django-datatables-view

  2. 前端配置-JS部分


    $('#mytable').DataTable({

        "paging": true,

        "lengthChange": true,

        "searching": true,

        "ordering": true,

       "info": true,

        "autoWidth": true,

        "lengthMenu": [[20,50,100, -1], [20,50,100, "All"]],

        "sPaginationType": "full_numbers",

        "bProcessing": true, //開(kāi)啟讀取服務(wù)器數(shù)據(jù)時(shí)顯示正在加載中……特別是大數(shù)據(jù)量的時(shí)候,開(kāi)啟此功能比較好

        "bServerSide": true, //開(kāi)啟服務(wù)器模式 

        "sAjaxSource": "{% url 'proxy_list_json' %}", //給服務(wù)器發(fā)請(qǐng)求的url


      });

  3. django 后臺(tái)響應(yīng)部分

    from django_datatables_view.base_datatable_view import BaseDatatableView

    def proxyAdmin(req):

        #rows=Proxy.objects.all()[:100]

        return render_to_response('data/proxyadmin.html')


    class ProxyListJson(BaseDatatableView):

        # The model we're going to show

        model = Proxy #要分頁(yè)的類(lèi)


        # define the columns that will be returned

        columns = ['ip', 'description', 'score', 'logdate'] #需要顯示的字段


        # 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 = ['ip','description', 'score', 'logdate'] #排序


        # 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):

                return super(ProxyListJson, self).render_column(row, column)


        def filter_queryset(self, qs):

            # use parameters passed in GET request to filter queryset

            qs_params = None

            search = self.request.GET.get(u'sSearch', None)

            if search: #模糊搜索

                q = Q(ip__contains=search)|Q(description__contains=search)

                qs_params = qs_params | q if qs_params else q

            

                qs = qs.filter(qs_params)


            return qs

  4. URLS配置

    url(r'^paging_proxy/$', ProxyListJson.as_view(), name='proxy_list_json'),

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI