my project is web
my app is personal
app->models.py
class UserInfo(models.Model): name = models.CharField(max_length=30) email = models.EmailField() memo = models.ImageField()# .CharField(max_length=300) # upload_to='/assets' date_time = models.DateTimeField(auto_now=True, editable=False) class Meta: verbose_name_plural = '用户信息' # modify
app->views.py
import pyecharts as pe from django.shortcuts import render from django.template import loader from pyecharts.constants import DEFAULT_HOST from personal.models import UserInfo from django.http import HttpResponse def create_bar(): user_list = UserInfo.objects.all().values_list("name", "email", "memo") page = pe.Page() name = ["服装"] name2 = ["电器"] x = list(user_list[0]) y = ["50", 200, 100] y2 = ["50", 300, 1400] # bar bar = pe.Bar("我的第一个图表", "这里是副标题", width=600, height=300) bar.add(name, x, y) bar.add(name2, x, y2) page.add(bar) # line line = pe.Line('Demo Line', "这里是副标题", width=600, height=300) line.add(name, x, y) line.add(name2, x, y2) page.add(line) return page def pyEharts(request): template = loader.get_template('personal/pyecharts.html') page = create_bar() context = dict( myechart=page.render_embed(), host=DEFAULT_HOST, script_list=page.get_js_dependencies() ) return HttpResponse(template.render(context, request))
app->urls.py
from django.conf.urls import url from . import views urlpatterns = [ url(r'^pe$', views.pyEharts, name='py'), ]
app->templates->app->pyecharts.html
<!-- myechartsite/djoncharts/templates/djoncharts/pyecharts.html --> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Proudly presented by PycCharts</title> {% for jsfile_name in script_list %} <script src="{{host}}/{{jsfile_name}}.js"></script> {% endfor %} </head> <body> {{myechart|safe}} </body> </html>
访问地址:http://127.0.0.1:8000/pe
reference:http://pyecharts.org/#/zh-cn/django
django and echarts: https://segmentfault.com/a/1190000011448528
django and database: http://blog.csdn.net/zuimei_forver/article/details/50372267