前几天,一直在研究django框架,发现它自身封装了很多有用的API,很有意思。比如,数据库操作基本的创表,查询,插值,更新,删除都有,很方便,再加上json库可以直接将数据json化,通过服务器传给前端并显示,连数据库也很快,响应迅速。
django 操作mysql数据库
取数据:
1 from blog.models import SfhdPredictDataTest
2 w = SfhdPredictDataTest.objects.all().values('time')
3
4 print(w)
…: <QuerySet [{‘time’: 1470758979}, {‘time’: 1470758982}, {‘time’: 1470758985}, {‘time’: 1470758988}
…: , {‘time’: 1470758991}, {‘time’: 1470758994}, {‘time’: 1470758997}, {‘time’: 1470759000}, {‘time’
…: : 1470759003}, {‘time’: 1470759006}, {‘time’: 1470759009}, {‘time’: 1470759012}, {‘time’: 1470759
…: 015}, {‘time’: 1470759018}, {‘time’: 1470759021}, {‘time’: 1470759024}, {‘time’: 1470759027}, {‘t
…: ime’: 1470759030}, {‘time’: 1470759033}, {‘time’: 1470759036}, ‘…
…:
1 w = SfhdPredictDataTest.objects.all().values_list(‘time‘,‘sample_model_predict_1‘ )
print(w)
…:
…: <QuerySet [(1470758979, 351.844), (1470758982, 351.353), (1470758985, 350.851), (1470758988, 350.
…: 244), (1470758991, 350.052), (1470758994, 350.084), (1470758997, 350.122), (1470759000, 350.203),
…: (1470759003, 350.205), (1470759006, 350.208), (1470759009, 350.005), (1470759012, 349.716), (147
…: 0759015, 349.6), (1470759018, 349.612), (1470759021, 349.568), (1470759024, 349.421), (1470759027
…: , 349.114), (1470759030, 348.792), (1470759033, 348.751), (1470759036, 348.777), ‘…(remaining e
…: lements truncated)…’]>
1.返回列表类型
1 lists = Chart.objects.values_list('time','predict')
2
3 list(lists)
2.返回字典类型
1 lists = Chart.objects.values(‘time‘,‘predict‘)
list(lists)r2
django引用app中的templates:
其中static为要加载的js,json文件
templates中加一个app的名字的文件夹,里面放html防止templates文件重名读错。
这是我最近遇到的一些问题,和解决的方法,记我踩过的坑