【问题标题】:Django: All Static Files 404 (but findstatic is able to locate them)Django:所有静态文件 404(但 findstatic 能够找到它们)
【发布时间】:2015-12-07 05:18:04
【问题描述】:

我刚刚开始了一个新的 Django 项目并添加到我的静态文件中以尝试加载我的模板。

但是,每当我加载模板时,我的所有静态文件都会给我一个 404 错误。

但是,当我使用 findstatic 时,它是成功的:

(AlmondKing) C:\Projects\AlmondKing>python manage.py findstatic images/logo.png  --verbosity 2

Found 'images/logo.png' here:
  C:\Projects\AlmondKing\AlmondKing\InventoryLogs\static\images\logo.png
  C:\Projects\AlmondKing\AlmondKing\FinancialLogs\static\images\logo.png
Looking in the following locations:
  C:\Projects\AlmondKing\AlmondKing\static
  C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\contrib\admin\static
  C:\Projects\AlmondKing\AlmondKing\InventoryLogs\static
  C:\Projects\AlmondKing\AlmondKing\FinancialLogs\static

这把我逼疯了。我已经尝试将静态文件放在所有相关位置,但它不会通过 runserver 抓取它们。关于罪魁祸首的任何想法?

我的设置:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'AlmondKing.InventoryLogs',
    'AlmondKing.FinancialLogs',
)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

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

【问题讨论】:

  • 添加一些关于您如何尝试访问模板中的静态文件以及萤火虫(或其他检查员)说什么的信息。

标签: python django django-staticfiles


【解决方案1】:

感谢 chem1st 的提示,我将模板转换为模板中的 {% loadstatic %} 语法,当前为recommended in the docs

它现在正在工作。

【讨论】:

    【解决方案2】:

    我怀疑你需要

    $ manage.py collectstatic 
    

    ..

    您还需要设置一个 STATIC_ROOT

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 2019-01-12
      • 2021-01-25
      • 2017-09-13
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 2015-12-01
      相关资源
      最近更新 更多