python - 为什么此图未显示在模板中,但如果直接在 Django 应用程序中查看 URL 则工作正常?

标签 python django plot mpld3

我的问题

我正在我的 Django 应用程序中创建一个交互式绘图。我已经创建了一个绘图模板 View 和一个仅生成绘图本身的 View ,以便我可以使用 <img src= "{% url 'ozmod:plot' %}"/>使用精美的绘图模板渲染绘图。

如果我导航到分配给绘图模板 View 的 URL,我会看到一个损坏的图像链接,但导航栏和所有内容都扩展得很好。如果我直接导​​航到仅查看绘图的 URL,则绘图显示正常,但当然,导航栏和 page.html 中的所有内容都不会扩展。我已经包含了错误和代码的屏幕截图:

views.py
class PlotView(generic.TemplateView):
    template_name = 'ozmod/plot.html'

def RunOZCOT(request):
    fig = plt.figure()
    x = range(20)
    y = [i for i in range(20)]
    random.shuffle(y)
    plot = plt.plot(x,y)
    g = mpld3.fig_to_html(fig)
    return HttpResponse(g)

urls.py
app_name = 'ozmod'
urlpatterns = [
    path('', views.HomePageView.as_view(), name='home'),
    path('metfiles/', views.MetIndexView.as_view(), name='metindex'),
    path('metfiles/<int:pk>/', views.DetailView.as_view(), name='detail'),
    path('runmodel/', views.PlotView.as_view(), name = 'runmodel'),
    path('plot/', views.RunOZCOT, name = 'plot'),
]

plot.html

{% extends "ozmod/page.html" %}

{% block content %}

<img src= "{% url 'ozmod:plot' %}"/> 
<!-- http://myserver:8000/ozmod/plot/ -->

{% endblock %}

page.html ...为了清晰起见缩写

{% load static %}

<!DOCTYPE html>
<head>
  <title>OZCOT interactive</title>
</head>
<body>

<ul class="nav nav-pills">
  <li class="{% if request.resolver_match.url_name == "home" %}active{% endif %}"><a href="{% url 'ozmod:home' %}">Home</a></li>
  <li class="{% if request.resolver_match.url_name == "metindex" %}active{% endif %}"><a href="{% url 'ozmod:metindex' %}">MetIndex</a></li>
  <li class="{% if request.resolver_match.url_name == "runmodel" %}active{% endif %}"><a href="{% url 'ozmod:runmodel' %}">Plot</a></li>
</ul>

<div class="container-fluid text-center">
    {% block content %}{% endblock content %}
</div>

<nav class="navbar navbar-default navbar-fixed-bottom">
        <div class="container">
            <p>This is a test</p>
        </div>
      </nav>

</body>
</html>

这就是 myserver:8000/ozmod/runmodel/的样子

enter image description here

这就是 myserver:8000/ozmod/plot/的样子

enter image description here

缺少什么?

因此,绘图工作正常,但当我在主绘图模板中引用提供该 View 的 url 时,该绘图不会显示。我错过了什么?

<小时/>

编辑:2018-03-23

使用<embed>不是<img> .

问题在于使用 <img>而不是<embed> 。通过嵌入,交互功能保持响应。

正确的views.py:

{% extends "ozmod/page.html" %}

{% block content %}

<embed src="{% url 'ozmod:plot' %}" width="800" height="600">

{% endblock %}

结果:

enter image description here

最佳答案

我不太明白什么mpld3.fig_to_html()确实 - 文档非常稀疏,我对 matplotlib 或 mpld3 一无所知 - 但它似乎返回某种 HTML。它不能作为图像的 src,它需要图像格式(gif/png/jpeg 等)的单个文件。

您需要使用 matplotlib 以该格式保存绘图并在 HttpResponse 中返回它。像这样的东西(记住我根本不了解图书馆):

plot = plt.plot(x,y)
response = HttpResponse()
plot.savefig(response)
return response

关于python - 为什么此图未显示在模板中,但如果直接在 Django 应用程序中查看 URL 则工作正常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49352276/

相关文章:

python - 如何将_id设置为32位整数?

python - 每 k 圈更换计数

python - 使用随机库在 python 中生成随机整数失败

java - MPAndroidChart - 删除实时图表中的旧数据点

r - Visnetwork 图未保存为 png 图像

python - Matplotlib:如何在 x 轴上绘制一 strip 有分类数据的线?

python - 继续追加到列表中,直到达到字符数

django - 在 Django Admin 中显示我的模型数据的子集

jquery - 通过 jQuery 提交后显示 Django 表单的验证错误 `.ajax`

javascript - 处理从 Django/JQuery 到 javascript Urllib3 的响应对象