我正在将Django的ajax从普通的html数据表转换为数据表(因为加载大型数据后,具有拉伸的页面并花费时间来获取所有数据)
这是我的代码:
model.py:
class myModel(models.Model):
date = models.CharField(max_length=8)
rnc = models.CharField(max_length=8,primary_key=True)
mo = models.CharField(max_length=100)
parameterid = models.CharField(max_length=100)
before_value = models.CharField(max_length=40)
class Meta:
db_table = "changed_para"
urls.py
urlpatterns = [
path('changed_para_json', views.changed_para_json, name='my_ajax_url'),
]
views.py
from django.http import HttpResponse
from django.core import serializers
from polls.models import myModel
def changed_para_json(request):
if request.POST.get("fetchdata"):
Date=request.POST["Date"]
RNC=request.POST["RNC"]
MO=request.POST["MO"]
parameterid=request.POST["parameterid"]
else:
Date='2018-07-20'
RNC='RRRNCH02'
MO='CORRMALGOSWITCH'
parameterid='DRASWITCH_DRA_VOICE_SAVE_CE_SWITCH'
current_time=datetime.date.today()
para = myModel.objects.using('rnc_parameters').all()
json = serializers.serialize('json', para)
object_list = myModel.objects.using('rnc_parameters').all() #or any kind of queryset
json = serializers.serialize('json', object_list)
return HttpResponse(json, content_type='application/json')
changed_para_json.html
</form>
<br>
<table id="tableid_1" cellpadding="0" cellspacing="0" border="0">
<thead class="thead-inverse">
<tr>
<th><font size="2">Date</th>
<th><font size="2">RNC</th>
<th><font size="2">MO</th>
<th><font size="2">parameterid</th>
<th><font size="2">value Before</th>
</tr>
</thead>
<tbody> </tbody>
</table>
<br>
<br>
<script type="text/javascript" language="javascript" class="init">
$(document).ready(function() {
$('#tableid_1').dataTable( {
"processing": true,
"ajax": {
"processing": true,
"url": "{% url 'my_ajax_url' %}",
"dataSrc": ""
},
"columns": [
{ "data": "fields.date" },
{ "data": "pk" },
{ "data": "fields.mo" },
{ "data": "fields.parameterid" },
{ "data": "fields.before_value" },
]
} );
} );
</script>
问题是,我从输出中的views.py获取所有序列化的json输出。这是web中的输出:
[{"model": "polls.myModel", "pk": "KRRNCH03", "fields": {"date": "2018-07-09", "mo": "FRC", "parameterid": "ECN0THSFOR2MSTO10MS", "before_value": "37"}}, {"model": "polls.myModel", "pk": "KRRNCH03", "fields": {"date": "2018-07-09", "mo": "FRC", "parameterid": "ECN0THSFOR2MSTO10MSCSPS", "before_value": "37"}}, {"model": "polls.myModel", "pk": "RRRNCH02", "fields": {"date": "2018-07-11", "mo": "INTERRATHOCOV", "parameterid": "INTERRATCSTHD2DECN0", "before_value": "-17"}}, {"model": "polls.myModel", "pk": "RRRNCH02", "fields": {"date": "2018-07-11", "mo": "INTERRATHOCOV", "parameterid": "INTERRATCSTHD2FECN0", "before_value": "-14"}}, {"model": "polls.myModel", "pk": "RRRNCH02", "fields": {"date": "2018-07-11", "mo": "UMULTIRABHOCOV", "parameterid": "CSPSMRABTHD2DECN0", "before_value": "-15"}}, {"model": "polls.myModel", "pk": "RRRNCH02", "fields": {"date": "2018-07-11", "mo": "UMULTIRABHOCOV", "parameterid": "CSPSMRABTHD2FECN0", "before_value": "-12"}}, {"model": "polls.myModel", "pk": "RRRNCH03", "fields": {"date": "2018-07-11", "mo": "INTERRATHOCOV", "parameterid": "INTERRATCSTHD2DECN0", "before_value": "-17"}}, {"model": "polls.myModel", "pk": "RRRNCH03", "fields": {"date": "2018-07-11", "mo": "INTERRATHOCOV", "parameterid": "INTERRATCSTHD2FECN0", "before_value": "-14"}}, {"model": "polls.myModel", "pk": "RRRNCH03", "fields": {"date": "2018-07-11", "mo": "UMULTIRABHOCOV", "parameterid": "CSPSMRABTHD2DECN0", "before_value": "-15"}}, {"model": "polls.myModel", "pk": "RRRNCH03", "fields": {"date": "2018-07-11", "mo": "UMULTIRABHOCOV", "parameterid": "CSPSMRABTHD2FECN0", "before_value": "-12"}}, {"model": "polls.myModel", "pk": "TERNCH11", "fields": {"date": "2018-07-11", "mo": "RRCTRLSWITCH", "parameterid": "OPTIMIZATIONSWITCH4_SCM_CU_CROSS_SWITCH", "before_value": "0"}}, {"model": "polls.myModel", "pk": "TERNCH12", "fields": {"date": "2018-07-11", "mo": "RRCTRLSWITCH", "parameterid": "OPTIMIZATIONSWITCH10_CS_PAGING_OVERLAP_RRC_EST_SWITCH", "before_value": "0"}}, {"model": "
如何解决它的表格显示方式?