• jQuery DataTables && Django serializer


    jQuery DataTables

    • https://www.datatables.net

    本文参考的官方示例

    • http://datatables.net/release-datatables/examples/ajax/objects.html

    关于Python序列化

    • http://www.cnblogs.com/linyawen/archive/2012/03/22/2411381.html

    Django中的Models序列化

    • http://www.weiguda.com/blog/19/

    Python的super用法

    • http://www.cnblogs.com/dkblog/archive/2011/02/24/1980654.html

    models.py

    from django.db import models
    from django.contrib import admin
    
    # Create your models here.
    class BankData(models.Model):
        acc_no = models.CharField(max_length=45,primary_key=True)
        name   = models.CharField(max_length=45,blank=True,default='')
        bank   = models.CharField(max_length=45,blank=True,default='')
    
    class BankDataAdmin(admin.ModelAdmin):
        list_display = ('acc_no',)
    
    admin.site.register(BankData, BankDataAdmin)

    serializers.py

    from rest_framework import serializers
    from myapp.models import BankData
    
    class BankDataSerializer(serializers.ModelSerializer):
        class Meta:
            model = BankData
            fields = ('acc_no','name','bank')

    views.py

    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    
    from myapp.models import BankData
    from django.http import HttpResponse
    from myapp.serializers import BankDataSerializer
    from rest_framework.renderers import JSONRenderer
    
    #json
    class JSONResponse(HttpResponse):
        def __init__(self, data, **kwargs):
            content = JSONRenderer().render(data)
            kwargs['content_type'] = 'application/json'
            super(JSONResponse, self).__init__(content,**kwargs)
    
    #jsonp
    class JSONPResponse(HttpResponse):
        def __init__(self, data, callback='callback',**kwargs):
            content = JSONRenderer().render(data)
            jsonp_content = callback+ "(" + content + ")"
            kwargs['content_type'] = 'application/json'
            super(JSONPResponse, self).__init__(jsonp_content,**kwargs)
    
    #json for DataTables
    class JSONDTResponse(HttpResponse):
        def __init__(self, data, **kwargs):
            content = JSONRenderer().render(data)
            dt_content = "{"data":" + content + "}"
            kwargs['content_type'] = 'application/json'
            super(JSONDTResponse, self).__init__(dt_content,**kwargs)
    
    
    def datas(request):
        bds = BankData.objects.all()
        serializer = BankDataSerializer(bds,many=True)
        return JSONDTResponse(serializer.data)

    bank_data.html

    <!DOCTYPE html>
    <head>
        <title>银行信息</title>
        <link rel="stylesheet" type="text/css" href="/static/css/jquery.dataTables.min.css">
        <script type="text/javascript" src="/static/js/jquery.js"></script>
        <script type="text/javascript" src="/static/js/jquery.dataTables.min.js"></script>
    </head>
    <body>
    <script>
        $(document).ready(function() {
            $('#example').dataTable( {
                "bProcessing":true,
                "ajax":'/myapp/datas/',
                "columns":
                [
                    {'data':'acc_no'},
                    {'data':'name'},
                    {'data':'bank'},
                ]
            });
        });
    </script>
    <table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>账号</th>
                <th>姓名</th>
                <th>银行</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>账号</th>
                <th>姓名</th>
                <th>银行</th>
            </tr>
        </tfoot>
    </table>
    </body>

    PS.Django中快速将Models变成字典对象

    from django.forms.models import model_to_dict
    from django.contrib.auth.models import User
    
    us = User.objects.all()
    print model_to_dict(us[0])
  • 相关阅读:
    Myeclipse10 + JBPM4.4 环境搭建图文教程
    关于ztree异步加载的问题(二)
    ztree学习之异步加载节点(一)
    【LeetCode】Search a 2D Matrix
    【LeetCode】Merge Sorted Array
    【LeetCode】Search for a Range
    一位阿里导师给大学生的忠告
    Java 基础
    java XML-RPC
    java web service
  • 原文地址:https://www.cnblogs.com/flowjacky/p/4537408.html
Copyright © 2020-2023  润新知