View:
视图:
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:
image.html:
<img src="{{ path }}" />
urls.py:
urls.py:
url(r'^blah$', blah_view, name="blah")
If I try to navigate to /blah, I get a 404 error when trying to render the image. I'm stumped. The image is in the correct path and exists.
如果我尝试导航到/ blah,我在尝试渲染图像时会出现404错误。我很难过。图像位于正确的路径中并存在。
I get a 404 error that 127.0.0.1:8000/home/templates/home/blah.png not found.
我收到404错误,找不到127.0.0.1:8000/home/templates/home/blah.png。
How can I display my generated image? Thanks in advance.
如何显示生成的图像?提前致谢。
1 个解决方案
#1
1
It will be a better idea, to create your graph and store it in Media folder. You can then set your MEDIA_URL and MEDIA_ROOT. Django does not serve static file directly from the folders.
创建图表并将其存储在Media文件夹中会更好。然后,您可以设置MEDIA_URL和MEDIA_ROOT。 Django不直接从文件夹中提供静态文件。
You can then configure your url.py to
然后,您可以将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)
#1
1
It will be a better idea, to create your graph and store it in Media folder. You can then set your MEDIA_URL and MEDIA_ROOT. Django does not serve static file directly from the folders.
创建图表并将其存储在Media文件夹中会更好。然后,您可以设置MEDIA_URL和MEDIA_ROOT。 Django不直接从文件夹中提供静态文件。
You can then configure your url.py to
然后,您可以将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)