查看:
import numpy as np
import matplotlib.pyplot as plt
import os
def blah_view(request):
# BUILD GRAPH
fname = "blah.png"
path = "home/templates/home"
fullpath = os.path.join(path, fname)
# 7 different groups
ind = np.arange(7)
width = 0.35
fig, ax = plt.subplots()
# code omitted. Generate graph here
plt.savefig(fullpath)
return render_to_response("image.html", {"path": fullpath})
image.html:
<img src="{{ path }}" />
urls.py:
url(r'^blah$', blah_view, name="blah")
如果我尝试导航到/ blah,我在尝试渲染图像时出现404错误。我很难过。图像位于正确的路径中并存在。
我收到404错误,找不到127.0.0.1:8000/home/templates/home/blah.png。
如何显示生成的图像?提前致谢。
答案 0 :(得分:1)
创建图表并将其存储在Media文件夹中会更好。然后,您可以设置MEDIA_URL和MEDIA_ROOT。 Django不直接从文件夹中提供静态文件。
然后,您可以将url.py配置为
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)