from jinja2 import Environment,FileSystemLoader from django.shortcuts import HttpResponse from pyecharts.globals import CurrentConfig from pyecharts import options as op from pyecharts.charts import Bar CurrentConfig.GLOBAL_ENV=Environment(loader=FileSystemLoader("./templates")) def index(request): c=( Bar() .add_xaxis(['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']) .add_yaxis('商家A',[5,20,36,10,75,90]) .add_yaxis('商家B',[15,25,16,55,48,8]) .set_global_opts(title_opts=op.TitleOpts(title='打压图',subtitle='我是tx'), yaxis_opts=op.AxisOpts(name='销售额(万元)', name_location='center', name_gap=30)) ) return HttpResponse(c.render_embed())
时间: 2024-02-14 19:10:56 浏览: 25
这段代码的问题在于返回的 `HttpResponse` 对象中,缺少了 `text/html` 类型的 `content_type` 参数。正确的代码应该像这样:
```
from jinja2 import Environment,FileSystemLoader
from django.shortcuts import HttpResponse
from pyecharts.globals import CurrentConfig
from pyecharts import options as op
from pyecharts.charts import Bar
CurrentConfig.GLOBAL_ENV=Environment(loader=FileSystemLoader("./templates"))
def index(request):
c=(
Bar()
.add_xaxis(['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子'])
.add_yaxis('商家A',[5,20,36,10,75,90])
.add_yaxis('商家B',[15,25,16,55,48,8])
.set_global_opts(title_opts=op.TitleOpts(title='打压图',subtitle='我是tx'),
yaxis_opts=op.AxisOpts(name='销售额(万元)',
name_location='center',
name_gap=30))
)
return HttpResponse(c.render_embed(), content_type='text/html')
```
在 `HttpResponse` 中添加 `content_type='text/html'` 参数,即可正常返回渲染结果。