我是python和Django的新手,但是有一些代码可以在显示我的图形时用作单独的弹出窗口。我在HTML页面中调用以下代码行:
{% block content %}
<h1>Title: {{ report.title }}</h1>
<div>
{{ report.show_graph }} //THIS PORTION IS CALLING THE GRAPH WHICH IS RESULTING IN A POPUP OF IT IN A SEPARATE WINDOW
</div>
<p><strong>Summary:</strong> {{ report.summary }}</p>
<p>Date Created: {{ report.date_created }}</p>
{% endblock %}
它触发一个请求并调用图形,但作为单独的独立窗口。 我正在调用的代码是:
class Report(models.Model):
"""Model representing a report"""
title = models.CharField(max_length=200)
date_created = models.DateField("Date", default=datetime.date.today)
summary = models.TextField(max_length=1000, help_text='Enter a brief summary of the report.')
def __str__(self):
"""String for representing the Model Object"""
return self.title
def get_absolute_url(self):
"""Returns the url to access a digital record of this Report"""
return reverse('report-detail', args=[str(self.id)])
def show_graph(self):
style.use('ggplot')
pfizer_data = quandl.get("NSE/PFIZER", authtoken="....")
pfizer_data['Close'].plot()
return plt.show()
我试图将图形返回为HTML,以便将其嵌入div中而不是单独的窗口中。谢谢。
答案 0 :(得分:0)
采用其他路由并使用了mpld3库。参考替代解决方案:Django Rendering Matplotlib chart in HTML using mpld3
答案 1 :(得分:0)