我是python,django和javascript的新手。在我的localhost网站上显示任何东西都是一个突破,但现在我试图让它显示为数据表,它只是填充为我认为的原始json数据。我猜这可能是导致这种情况的模板中的东西,但我很新手所以非常感谢任何帮助。
Models.py
from django.db import models
class pitcher(models.Model):
id = models.IntegerField(primary_key=True)
player_name = models.CharField('pitcher name', max_length=255, default='null')
pitch_type = models.CharField(max_length=255, default='null')
game_date = models.DateField(null=True, blank=True)
release_speed = models.FloatField()
pfx_x = models.FloatField()
pfx_z = models.FloatField()
spin_rate = models.FloatField()
estimated_ba_using_speedangle = models.FloatField()
estimated_woba_using_speedangle = models.FloatField()
babip_value = models.FloatField()
Usage = models.FloatField()
urls.py
from django.conf.urls import url
from analyzer import views
from analyzer.views import pitcherlist
import json
urlpatterns = [
url(r'^$', pitcherlist.as_view(), name='pitcherlist_json'),
]
views.py
from django.shortcuts import render
from .models import pitcher
from django_datatables_view.base_datatable_view import BaseDatatableView
import json
from django.http.response import HttpResponse
class pitcherlist(BaseDatatableView):
model = pitcher
columns = ['player_name', 'game_date','pitch_type', 'pfx_x']
max_display_length = 500
模板
<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"/>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>
<head>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-12">
<div class="well">
<table id="pitcherlist" class="display responsive" width="100%">
<thead>
<tr>
<th width="5%">player_name</th>
<th width="5%">game_date</th>
<th width="5%">pitch_type</th>
<th width="5%">pfx_x</th>
<th width="5%"></th>
</tr>
</thead>
</table>
</div>
</div>
</div>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
var oTable: $('#pitcherlist').dataTable({
"processing": true,
"serverSide": true,
"ajaxSource": {% url 'pitcherlist_json' %}
});
</script>
</body>
</html>
这是输出:
{“draw”:0,“recordsTotal”:171,“recordsFiltered”:171,“data”: [[“Hector Neris”,“2017-07-31”,“FF”, - 0.6053],[“Hector Neris”, “2017-07-31”,“FS”,-1.1989],[“Hector Neris”,“2017-07-31”,“FF”, -0.7938],[“Hector Neris”,“2017-07-31”,“FF”, - 0.7876],[“Hector Neris”,“2017-07-31”,“FF”, - 0.8419],[“ Hector Neris“,”2017-07-31“, “FF”, - 0.9699],[“Hector Neris”,“2017-07-31”,“FF”, - 0.8357], [“Hector Neris”,“2017-07-31”,“FS”, - 0.8772],[“Hector Neris”, “2017-07-31”,“FF”, - 0.6558],[“Hector Neris”,“2017-07-31”,“FF”, -0.6579]],“结果”:“ok”}
答案 0 :(得分:0)
检查一下。 How to build up a HTML table with a simple for loop in Jinja2?您将必须执行以下操作:
<table>
{% for x in pitcherlist%}
<tr>
<th width="5%">player_name</th>
<th width="5%">game_date</th>
<th width="5%">pitch_type</th>
<th width="5%">pfx_x</th>
</tr>
<td>{{ x.player_name}}</td>
<td>{{ x.game_date}}</td>
<td>{{ x.pitch_type}}</td>
<td>{{ x.pfx_x}}</td>
</tr>
{% endfor %}
</table>