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])
上一篇:jQuery datatables


下一篇:ASP.NET MVC+EF在服务端分页使用jqGrid以及jquery Datatables的注意事项