【问题标题】:STATICFILES on Django 1.6.2Django 1.6.2 上的静态文件
【发布时间】:2014-04-15 20:35:54
【问题描述】:

这是我的settings.py

STATIC_URL = '/static/'

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

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

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

我的结构是:

manage.py
myproject
-- __init__.py
-- settings.py
-- urls.py
-- wsgi.py
-- static
----- css
----- img
-------- logo.png
----- scripts

我在我的 html 文件中使用静态标签,如下所示:<img src="{{ STATIC_URL }}img/logo.png">

但是,它是:

[Error] Failed to load resource: the server responded with a status of 500 (Internal Server Error) (logo.png, line 0)

在 Django 1.6.2 版本上我真的无法理解这个问题。

有人可以帮我解决这个问题吗?

提前致谢!

【问题讨论】:

  • 您的错误看起来不像 Django 错误...此外,如果您查看呈现页面的源(在您的浏览器中),它试图获取的图像的 URL 是什么?
  • <img src="/static/img/logo.png">

标签: python django django-settings


【解决方案1】:

您需要确保django.contrib.staticfiles 应用在INSTALLED_APPS 中列出:

INSTALLED_APPS = (
    ...
    'django.contrib.staticfiles'
    ...
)

最后,您需要确保在settings.py 中将DEBUG 设置为True

编辑:

尝试从STATICFILES_FINDERS 中删除'django.contrib.staticfiles.finders.DefaultStorageFinder'。然后运行$ python manage.py findstatic img/logo.png

【讨论】:

  • django.contrib.staticfiles 在那里,DEBUGTrue
  • 尝试运行$ python manage.py findstatic img/logo.png,看看它是否正确找到它。
  • 当我运行它时,我得到这个错误:django.core.exceptions.ImproperlyConfigured: The storage backend of the staticfiles finder <class 'django.contrib.staticfiles.finders.DefaultStorageFinder'> doesn't have a valid location.
猜你喜欢
  • 1970-01-01
  • 2014-02-04
  • 2012-10-12
  • 2014-08-27
  • 2019-07-11
  • 2020-11-06
  • 2021-06-03
  • 2014-02-26
  • 2021-03-18
相关资源
最近更新 更多