[英]Django returning 404 for static files
When running a Django project on my local Mac server I get 404 errors when trying to access static files. 在本地Mac服务器上运行Django项目时,尝试访问静态文件时出现404错误。
My directory structure looks like: 我的目录结构如下:
myapp/static
In my settings.py I have tried a variety of combinations of the following: 在我的settings.py中,我尝试了以下各种组合:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATICFILES_DIRS = [
os.path.join(PROJECT_ROOT, 'static'),
'/var/www/myapp/static',
'/pycharmprojects/myapp/static'
]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
)
For every use of a static file I get an error like this in PyCharm: 对于静态文件的每次使用,我在PyCharm中都会收到如下错误:
GET /static/img/pastedsvg%2017.svg HTTP/1.1" 404 101
And an error like this in Firefox: 并在Firefox中出现如下错误:
GET
http://127.0.0.1:8888/static/img/pastedsvg%2017.svg [HTTP/1.0 404 NOT FOUND 15ms]
I have looked at other answers to this problem in Stackoverflow and tried the suggestions but so far nothing works. 我在Stackoverflow中查看了针对此问题的其他答案,并尝试了建议,但到目前为止没有任何效果。
It is difficult to tell what exactly the issue might be, but here are some observations that may help: 很难确定问题到底是什么,但是以下一些观察可能会有所帮助:
STATIC_ROOT
is only used for collectstatic
. STATIC_ROOT
仅用于collectstatic
。 It is where collectstatic
will copy your static files when you run it from the command line ( python -m django collectstatic
). python -m django collectstatic
时, collectstatic
将在其中复制静态文件( python -m django collectstatic
)。 STATIC_ROOT
should not be included in STATICFILES_DIRS
as this would confuse collectstatic
since its job is to copy static files from your static directories to STATIC_ROOT
. STATIC_ROOT
不应包含在STATICFILES_DIRS
因为这会混淆collectstatic
因为它的工作是将静态文件从您的静态目录复制到STATIC_ROOT
。 AppDirectoriesFinder
is going to look for static files in a directory called static
under each of your apps. AppDirectoriesFinder
将在每个应用程序下的称为static
的目录中查找静态文件。 So, for example, if your project is located in /pycharmprojects/myapp/
, AppDirectoriesFinder
will look for static files in directories such as /pycharmprojects/myapp/anapp/static
and /pycharmprojects/myapp/anotherapp/static
(for each application listed in INSTALLED_APPS
) and copy them to STATIC_ROOT
. /pycharmprojects/myapp/
,则AppDirectoriesFinder
将在/pycharmprojects/myapp/anapp/static
和/pycharmprojects/myapp/anotherapp/static
目录中查找静态文件(针对INSTALLED_APPS
)并将其复制到STATIC_ROOT
。 FileSystemFinder
is going to use the values in STATICFILES_DIRS
to look for static files and copy them to STATIC_ROOT
(which is why your value for STATIC_ROOT
should not be listed in STATICFILES_DIRS
). FileSystemFinder
将使用STATICFILES_DIRS
的值来查找静态文件,并将其复制到STATIC_ROOT
(这就是为什么STATIC_ROOT
的值不应在STATICFILES_DIRS
列出)的原因。 collectstatic
as Django will look for files in their original location, but when not debug mode ( DEBUG = False
in your Django config file), you will need to run collectstatic
in order for Django to find your static files. collectstatic
静态文件,因为Django会在其原始位置查找文件,但是在非调试模式下(您的Django配置文件中的DEBUG = False
),您将需要运行collectstatic
以便Django查找您的静态文件。 So, from this, I suggest: 因此,我建议:
os.path.join(PROJECT_ROOT, 'static')
from your STATICFILES_DIR
STATICFILES_DIR
绝对删除os.path.join(PROJECT_ROOT, 'static')
/pycharmprojects/myapp/static
from your STATICFILES_DIR
if that points to the same location as os.path.join(PROJECT_ROOT, 'static')
STATICFILES_DIR
指向与os.path.join(PROJECT_ROOT, 'static')
相同的位置,则可能从/pycharmprojects/myapp/static
删除/pycharmprojects/myapp/static
DEBUG = True
in your Django config file), run collectstatic
from the command line so that Django can see your static files. DEBUG = True
),请从命令行运行collectstatic
,以便Django可以查看您的静态文件。 One other question. 另一个问题。 To which directory is your
BASE_DIR
pointing? 您的
BASE_DIR
指向哪个目录? It seems like PROJECT_DIR
might be redundant since it's pointing to where BASE_DIR
usually points. 似乎
PROJECT_DIR
可能是多余的,因为它指向BASE_DIR
通常指向的位置。
Hopefully something in this wall of text will be helpful. 希望本文中的内容会有所帮助。
Cheers! 干杯!
I don't know that the following is the correct answer, but I hope it will go well. 我不知道以下是正确的答案,但我希望它会顺利进行。
STATIC_URL = '/static/'
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATICFILES_DIRS = [
# os.path.join(PROJECT_ROOT, "static"),
os.path.join(BASE_DIR, "static"),
]
print("PROJECT_ROOT", PROJECT_ROOT)
print("BASE_DIR", BASE_DIR)
In this case, the value of the variable named PROJECT_ROOT is the same directory that exists setting file. 在这种情况下,名为PROJECT_ROOT的变量的值与设置文件存在的目录相同。 That is why it's not PROJECT_ROOT exactly.
这就是为什么它不是完全PROJECT_ROOT的原因。
You can execute when running server even if writing python-code in setting file. 即使在设置文件中写入python代码,也可以在运行服务器时执行。 So you can verify that the code was written in properly once again.
因此,您可以再次验证代码是否正确编写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.