Django quickly integrates echarts
1. First upload the result graph
2. Code implementation
1. Pycharm (Professional Edition) creates a Django project and completes the related configuration
(1) Create a new project with the name echartsdemo1
(2) Create a static directory under the project root directory— >store css, js, img
(3) Attention to configuration setting file
: STATICFILES_DIRS cannot be written wrong
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
2. File code:
(1) views.py in the app (model) directory
from django.shortcuts import render
//创建函数echarts
def echarts(request):
return render(request,'demo.html')
(2) demo.html in the templates directory
(echarts.min.js file can be downloaded from the official website)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ECharts</title>
<!-- 引入 echarts.js 引入静态文件的一种方法-->
{
% load static %}
<script src="{% static '/js/echarts.min.js' %}"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main" style="width: 600px;height:400px;"></div>
<script type="text/javascript">
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {
},
legend: {
data:['销量']
},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {
},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script>
</body>
</html>
(3) urls.py in the echartsdemo1 directory
from django.contrib import admin
from django.urls import path
from app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('demo/', views.echarts),
]
3. Start the project
Open the browser and enter: http://127.0.0.1:8000/demo/
OK~~