我正在尝试创建一个仪表板,使用plotly库来分析我的模型数据(文章)。
我的模板上没有显示Plotly柱状图,我想知道是否有问题,因为以下代码没有错误:
models.py
from django.db import models
from django.contrib.auth.models import User
import plotly.plotly as py
import plotly.graph_objs as go
class Article(models.Model):
user = models.ForeignKey(User, default='1')
titre = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=40)
likes = models.ManyToManyField(User, related_name="likes")
def __str__(self):
return self.titre
@property
def article_chart(self):
data = [
go.Bar(
x=[self.titre], #title of the article
y=[self.likes.count()] #number of likes on an article
)
]
plot_url = py.plot(data, filename='basic-bar')
return plot_url
仪表板.html
<div>{{ article.article_chart }}</div>
为什么条形图不可见?有什么建议吗?
article_chart
是Article
的一个属性,因此您需要在模板中引用它,如{{ article.article_chart }}
。 - Franey