Django找不到已经存在的静态文件

huangapple go评论70阅读模式
英文:

Django Can't Find a static file that's already there

问题

我在使用Django时遇到了一个非常奇怪的问题。基本上,我在<static/css>目录下有一个CSS静态文件,但终端显示了以下奇怪的消息:“GET /home/.../django/mysite/static/css/style.css HTTP/1.1” 404 1932,而实际上文件是存在的!

设置文件:

DEBUG = True
ALLOWED_HOSTS = ['*']
STATIC_URL = str(BASE_DIR) + '/static/'
STATIC_ROOT = str(BASE_DIR.joinpath('static'))
STATICFILES_DIR = (str(BASE_DIR.joinpath('static')),)

我已经使用了“python manage.py collectstatic”命令,但它没有起作用;我还查看了这个页面:https://docs.djangoproject.com/en/4.2/howto/static-files/,但对我没有解决方案... 请告诉我问题出在哪里!

英文:

I'm facing a very peculiar problem using Django. So basically I have a CSS static file inside <static/css> directory, and here's the strange message from terminal: "GET /home/.../django/mysite/static/css/style.css HTTP/1.1" 404 1932, while the file is actually there!

settings file:

DEBUG = True
ALLOWED_HOSTS = [&#39;*&#39;]
STATIC_URL = str(BASE_DIR) + &#39;/static/&#39; 
STATIC_ROOT = str(BASE_DIR.joinpath(&#39;static&#39;))
STATICFILES_DIR = (str(BASE_DIR.joinpath(&#39;static&#39;)),)

I have used the command "python manage.py collectstatic", but it didn't work; I also checked out this page: <https://docs.djangoproject.com/en/4.2/howto/static-files/> but there was no solution for me... Please tell me what's wrong!

答案1

得分: 1

尝试将您的STATIC_URL设置如下:

STATIC_URL = 'static/'

您当前是根据项目目录结构构建它,但网页需要它在网站上下文中,例如,domain.com/static/

英文:

try making your STATIC_URL as follows

STATIC_URL = 'static/'

You are currently constructing it out of your project directory structure, but the web page needs it in a website context, eg, domain.com/static/

huangapple
  • 本文由 发表于 2023年7月11日 00:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定