如何在django html模板中查看图像

时间:2014-07-24 01:26:59

标签: python django image static http-status-code-404

即使图像的网址准确无误,我仍然会收到404错误。是的,我查看了官方的django文档和大量的stackoverflow帖子。我无法从这些中弄明白。如果有人能够破译instructions are saying要做的事情,那么我将感激不尽,因为他们似乎缺少信息。

的index.html:

#I have tried static and staticfiles
{% load static %}
#a lot of code later...
<img border="0" src="{% static "logo.png" %}" alt="TMS" width="125" height="80">

目录结构:

projectname
    ->__init__.py, settings.py, ...
manage.py
db.sql3
app_name
    -> admin.py, models.py, views.py, templates->index.html, ...
static
    ->logo.png

我在settings.py以及&#39; django.contrib.staticfiles&#39;中唯一关于静态的事情:

STATIC_URL = '/static/'

我的views.py文件有索引函数,它返回HttpResponse(t.render(c)),其中t是index.html模板,c是Context。这可能是问题吗?

结果的网址是:http://127.0.0.1:8000/static/logo.png

1 个答案:

答案 0 :(得分:0)

您正在使用应用程序之外的静态目录(位于项目文件夹中),因此在settings.py中您还需要:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)