因为前几天用python爬取了1000天混合型基金的数据
就想着,反正也没事干
要不用python的django做个网页显示?
说干就干,想法就是django从数据库中取到数据
返回json,前端的echarts解析
并且生成可视化界面
1.启动django
写views文件
import json
from django.http import HttpResponse
from django.shortcuts import render
import pymysql
def testDB(request):
conn=pymysql.connect(host="127.0.0.1",user="root",password="密码",port=3306,database="fiance")
cursor=conn.cursor()
sql="""
SELECT * from sheet1 order by 近1周 DESC LIMIT 5"""
cursor.execute(sql)
result=cursor.fetchall()
return HttpResponse(json.dumps(result), content_type="application/json")
def displayJson(request):
return render(request,"display.html")
这里说一下 testDB是链接数据库,并且返回json数据
那么display这个视图呢就是单纯的显示,
然后我们去配置一下我们的路由
from django.urls import path
from .views import index,testDB,displayJson
app_name="article"
urlpatterns = [
path('DB/',testDB),
path('json/',displayJson)
<