django 网站为静态文件提供 404 错误
django website is giving 404 error for static files
Django 2 中的静态文件有问题。
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR + '/static/'
STATICFILES_DIR = [
(BASE_DIR + "static/"),
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
我的静态结构如下
我得到了 404 的静力学结果。尝试了不同的事情但没有成功。 btw collectstatic 已经执行。另一件事是当我在静态文件夹中进行更改时,collectstatic 没有收集任何东西。它说 "unmodified".
我是 Django 的新手。感谢您的帮助。
更新 2:
新结构如下
设置就像
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static' )
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_files')
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
当我尝试收集静态数据时,没有任何变化。好像没有收藏。和 404 returns 自然用于静态,因为它不收集。
这可能是一个转移注意力的问题,但在 Django docs 中,设置相对于您的 BASE_DIR 的路径的语法包括 os.path.join
,因此您的路径看起来像:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
编辑:我还刚刚注意到您引用了 DIR
而不是 DIRS
,这可能会引起悲伤。 templates 同上。
有 2 个问题:
1.是Digitiain在中指出的语法错误:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
2. 您不能将 STATIC_ROOT
中的路径添加到您的 STTATICFILES_DIRS
。所以如果你的 STTATICFILES_DIRS
像上面那样,STATIC_ROOT
就不可能是 os.path.join(BASE_DIR, "static")
.
第一点应该很清楚了。第二个一开始也让我感到困惑。但这实际上很有意义:
在STATICFILES_DIRS
中你告诉django去哪里寻找静态文件。这主要不是为了服务他们,而是为了收集他们!关键是在生产服务器上,django 不应该提供静态文件;此任务应由 Web 服务器处理。 Check the docs for details. The idea is that you run the command python manage.py collectstatic
and django will go through all the paths in STATICFILES_DIRS
, collect the static files and put them... - exactly! - in the STATIC_ROOT
folder. Now it becomes clear why the STATIC_ROOT
folder cannot really be part of STATICFILES_DIRS
as this would mean django would need to put the folder into itself. Again, this is in the case of a productive server. During development django can handle static files without help of a web server, here 更多来自文档。
我希望这有助于澄清 STATICFILES_DIRS
和 STATIC_ROOT
之间的关系。底线:为 STATIC_ROOT
选择不同的路径,你应该可以开始了!
举个例子,你可以有一个这样的项目结构:
base_dir/
- static/
- css/
- js/
- ...
- static_collected/
- ...
那么定义是有效的:
# settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static_collected")
Django 2 中的静态文件有问题。
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR + '/static/'
STATICFILES_DIR = [
(BASE_DIR + "static/"),
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
我的静态结构如下
我得到了 404 的静力学结果。尝试了不同的事情但没有成功。 btw collectstatic 已经执行。另一件事是当我在静态文件夹中进行更改时,collectstatic 没有收集任何东西。它说 "unmodified".
我是 Django 的新手。感谢您的帮助。
更新 2:
新结构如下
设置就像
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static' )
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_files')
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
当我尝试收集静态数据时,没有任何变化。好像没有收藏。和 404 returns 自然用于静态,因为它不收集。
这可能是一个转移注意力的问题,但在 Django docs 中,设置相对于您的 BASE_DIR 的路径的语法包括 os.path.join
,因此您的路径看起来像:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
编辑:我还刚刚注意到您引用了 DIR
而不是 DIRS
,这可能会引起悲伤。 templates 同上。
有 2 个问题:
1.是Digitiain在
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
2. 您不能将 STATIC_ROOT
中的路径添加到您的 STTATICFILES_DIRS
。所以如果你的 STTATICFILES_DIRS
像上面那样,STATIC_ROOT
就不可能是 os.path.join(BASE_DIR, "static")
.
第一点应该很清楚了。第二个一开始也让我感到困惑。但这实际上很有意义:
在STATICFILES_DIRS
中你告诉django去哪里寻找静态文件。这主要不是为了服务他们,而是为了收集他们!关键是在生产服务器上,django 不应该提供静态文件;此任务应由 Web 服务器处理。 Check the docs for details. The idea is that you run the command python manage.py collectstatic
and django will go through all the paths in STATICFILES_DIRS
, collect the static files and put them... - exactly! - in the STATIC_ROOT
folder. Now it becomes clear why the STATIC_ROOT
folder cannot really be part of STATICFILES_DIRS
as this would mean django would need to put the folder into itself. Again, this is in the case of a productive server. During development django can handle static files without help of a web server, here 更多来自文档。
我希望这有助于澄清 STATICFILES_DIRS
和 STATIC_ROOT
之间的关系。底线:为 STATIC_ROOT
选择不同的路径,你应该可以开始了!
举个例子,你可以有一个这样的项目结构:
base_dir/
- static/
- css/
- js/
- ...
- static_collected/
- ...
那么定义是有效的:
# settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static_collected")