I'm pretty new to python, django and javascript. It was a breakthrough to get anything at all to display on my localhost site, but now I'm trying to get it to display as a datatable and it is only populating as raw json data I think. I'm guessing it is probably something in the template that is causing this, but I'm pretty novice so would appreciate any help.
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
template
<!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>
This is the output:
{"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"}