[英]django website is giving 404 error for static files
Having problem with staticfiles in Django 2. 在Django 2中遇到staticfiles问题。
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',
]
And my static structure is like below 我的静态结构如下
I am getting 404 for statics. 我得到404的静态信息。 Tried different things but no success.
尝试了不同的事情,但没有成功。 btw collectstatic has been already performed.
btw collectstatic已经被执行。 Another thing is when i make changes in the static folder, collectstatic is not collecting anything.
另一件事是,当我在静态文件夹中进行更改时,collectstatic不会收集任何内容。 it says "unmodified".
它说“未修改”。
i am newbie in Django. 我是Django的新手。 appreciate for your help.
感谢您的帮助。
UPDATE 2: 更新2:
and the settings is like 设置就像
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',
]
When i try to collectstatic, there is no change. 当我尝试收集静电时,没有任何变化。 seems it is not collecting.
似乎没有收集。 and 404 returns for statics naturally since it is not collecting.
和404自然返回静态值,因为它没有收集。
There are 2 issues: 有两个问题:
1. is the syntax error that Digitiain pointed out in his answer : 1. Digitiain在回答中指出的语法错误:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
2. You cannot add the path in STATIC_ROOT
to your STTATICFILES_DIRS
. 2.您不能将
STATIC_ROOT
的路径添加到STTATICFILES_DIRS
。 So if your STTATICFILES_DIRS
looks like above, STATIC_ROOT
cannot be os.path.join(BASE_DIR, "static")
. 因此,如果您的
STTATICFILES_DIRS
如上所示,则STATIC_ROOT
不能为os.path.join(BASE_DIR, "static")
。
The first point should be fairly clear. 第一点应该很清楚。 The second also confused me in the beginning.
第二个开始时也让我感到困惑。 But it makes a lot of sense actually:
但这实际上很有意义:
In STATICFILES_DIRS
you tell django where to go and look for static files. 在
STATICFILES_DIRS
您告诉django去哪里寻找静态文件。 This not primarily to serve them, but to collect them! 这主要不是为了服务他们,而是为了收集他们! The point is that on a productive server, django is not supposed to server static files, this task should be handled by the web server.
关键是,在生产型服务器上,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! 这个想法是,您运行命令
python manage.py collectstatic
并且django将遍历STATICFILES_DIRS
所有路径,收集静态文件并将其放置...-完全是! - in the STATIC_ROOT
folder. -在
STATIC_ROOT
文件夹中。 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. 现在很清楚为什么
STATIC_ROOT
文件夹不能真正成为STATICFILES_DIRS
一部分,因为这意味着django需要将其放入文件夹中。 Again, this is in the case of a productive server. 同样,在生产服务器的情况下。 During development django can handle static files without help of a web server, here is more from the docs on that.
在开发过程中,django可以在没有Web服务器帮助的情况下处理静态文件,有关此文档的更多信息。
I hope that helps to clarify the relation between STATICFILES_DIRS
and STATIC_ROOT
. 我希望这有助于阐明
STATICFILES_DIRS
和STATIC_ROOT
之间的关系。 Bottom-line: Choose a different path for STATIC_ROOT
and you should be good to go! 底线:为
STATIC_ROOT
选择其他路径,您应该STATIC_ROOT
顺利!
As an example , you could have a project structure like this: 举例来说 ,您可能具有以下项目结构:
base_dir/
- static/
- css/
- js/
- ...
- static_collected/
- ...
Then it would be valid to define: 然后定义以下内容将是有效的:
# settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static_collected")
This might be a red herring, but in the Django docs the syntax for setting a path relative to your BASE_DIR includes os.path.join
, so yours would look like: 这可能是一条红色的鲱鱼,但是在Django docs中 ,用于设置相对于BASE_DIR的路径的语法包括
os.path.join
,因此您的样子如下:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
EDIT: I also just noticed that you referenced DIR
rather than DIRS
, which could be causing grief. 编辑:我也只是注意到您引用了
DIR
而不是DIRS
,这可能会引起悲伤。 Ditto with templates . 与模板同上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.