【发布时间】:2012-11-04 15:14:21
【问题描述】:
我已经查看了这里和文档中的几乎所有示例,但它根本不起作用
所以在我的 settings.py 文件中有
STATIC_ROOT = '/mattr/static/'
STATIC_URL = '/mattr/public/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',)
TEMPLATE_CONTEXT_PROCESSORS = ('django.core.context_processors.static',)
TEMPLATE_DIRS = ('mattr/public', )
基本上处理静态文件所需的一切。
在 urls.py 我有页面的正常模式(模板加载得很好)并有这个额外的行
urlpatterns += staticfiles_urlpatterns()
在 views.py 我有(这是主页):
def home(request):
t = get_template('index.html');
html = t.render(RequestContext(request))
return HttpResponse(html)
在模板文件 index.html 我有一行
<img src="{{ STATIC_URL }}media/images/Mattr1.png">
但它从不显示图像。即使我尝试直接在 http://127.0.0.1:8000/mattr/public/media/images/Mattr1.png 访问图像文件,它也会给我一个 Page Not Found 错误。 我有点困惑路径从哪里开始,但因为我的模板页面加载我认为我的路径是正确的
【问题讨论】:
-
如果
http://127.0.0.1:8000/mattr/public/media/images/Mattr1.png错误,您期望的网址是什么?这将有助于重新配置您的设置 -
@AamirAdnan 基本上来自我网站的根目录(manage.py 的位置),我的文件位于“/mattr/public/images/Mattr1.png”,所以我希望它能够显示出来
-
那么应该是这样的
<img src="{{ STATIC_URL }}images/Mattr1.png"> -
@AamirAdnan 抱歉,那里应该也有媒体(在文件路径中)