Tôi đang cố gắng tạo trang tổng quan nơi tôi có thể phân tích dữ liệu của mô hình (Điều) bằng cách sử dụng thư viện plotly.Làm thế nào để tạo biểu đồ với Plotly trên Django?
Các biểu đồ thanh Plotly không hiển thị trên mẫu của tôi, tôi tự hỏi nếu tôi làm điều gì sai vì không có lỗi với mã dưới đây:
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
dashboard.html
<div>{{ article.article_chart }}</div>
Tại sao biểu đồ thanh không hiển thị? Bất kì lời đề nghị nào ?
'article_chart' là thuộc tính của' Article', vì vậy bạn cần tham khảo nó như '{{article.article_chart}}' trong mẫu của bạn. – Franey