Django can';在模板中找不到静态图像

Django can';在模板中找不到静态图像,django,django-staticfiles,Django,Django Staticfiles,我已经看过了这里和文档中几乎所有的例子,但它根本不起作用 所以在我的settings.py文件中 STATIC_ROOT = '/mattr/static/' STATIC_URL = '/mattr/public/' STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder',)

我已经看过了这里和文档中几乎所有的例子,但它根本不起作用

所以在我的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中,我有(这是主页):

在模板文件index.html中,我有一行

<img src="{{ STATIC_URL }}media/images/Mattr1.png">

但它从不显示图像。甚至当我试图直接访问图像文件时http://127.0.0.1:8000/mattr/public/media/images/Mattr1.png 它给了我一个页面未找到的错误。
我有点困惑路径从何处开始,但由于我的模板页面加载,我想我的路径是正确的

请在您的设置中尝试以下操作。py:

import os
DIRNAME = os.path.dirname(__file__)

STATIC_ROOT = os.path.join(DIRNAME, 'static')

STATIC_URL = '/static/'
在模板中:

{{STATIC_URL}}css/etc...
您还将覆盖您的
模板\u上下文\u处理器
,请执行以下操作: 在您的settings.py中

import django.conf.global_settings as DEFAULT_SETTINGS

TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
"whatever_your_adding",
)

在路径前面使用“/”表示您是从根目录引用的,我想情况并非如此。试着这样做

STATIC_ROOT = 'mattr/static/'

当您谈论静态文件时,请执行以下操作:

STATIC_URL = '/static/' #or whatever you want

STATICFILES_DIRS = (
    '/path/to/static/root/directory/',
)
MEDIA_ROOT = '/media/' # or whatever you want

MEDIA_URL = '/path/to/media/root/directory'
别忘了coma或者django的管理员没有css。 完成后,无需更改
url.py中的任何内容

如果您正在谈论媒体,请执行以下操作:

STATIC_URL = '/static/' #or whatever you want

STATICFILES_DIRS = (
    '/path/to/static/root/directory/',
)
MEDIA_ROOT = '/media/' # or whatever you want

MEDIA_URL = '/path/to/media/root/directory'
并将其放在底部的
myproject.url

import settings
urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT,}),)
导入设置
urlpatterns+=模式(“”,url(r'^media/(?P.*)$,'django.views.static.service',{'document\u root':settings.media\u root,}),)

完成了

如果此
http://127.0.0.1:8000/mattr/public/media/images/Mattr1.png
有错吗?这将有助于重新配置您的settings@AamirAdnan基本上,从我的站点的根目录(manage.py的位置)来看,我的文件位于“/mattr/public/images/Mattr1.png”,所以我希望它能显示出来,所以它应该是这样的
@AamirAdnan抱歉,那里应该也有一个媒体(在文件路径中),所以静态根目录行,那么我应该把文件放在哪里呢?看起来它在路径的某个地方添加了一个额外的“static”,模板行也导致了一个语法错误,因为+不需要更改url.py中的任何内容。我已经将静态重定向添加到了我的url.py,这阻止了它的工作。