我是 python、django 和 javascript 的新手。让任何东西都显示在我的本地主机站点上是一个突破,但现在我正试图让它显示为数据表,我认为它只是填充为原始 json 数据。我猜这可能是模板中的某些内容导致的,但我是新手,所以非常感谢任何帮助。
模型.py
from django.db import models
类投手(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()
网址.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'),
]
View .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]], "result": "ok"}
最佳答案
检查一下。 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>
关于python - Django Datatables Views 不显示数据表,只显示 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45994887/