简体   繁体   English

django在寻找静态文件时,我不断收到404错误

[英]I keep getting a 404 error when django is looking for static files

I keep getting a 404 error when django is looking for static files. 当django在寻找静态文件时,我不断收到404错误。

settings.py settings.py

STATIC_ROOT = os.path.join(BASE_DIR, 'business', 'static_cdn')
STATIC_URL = '/static_cdn/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'business', 'static'),
]

project tree structure of app 应用程序的项目树结构

- business
-- migrations
--- ....
-- static_cdn
--- business
---- main.css
---- scripts.js
-- templates
--- business
---- base.html
---- home.html
-- templatetags
--- ...
-- __init__.py
-- admin.py
-- apps.py
-- models.py
-- tests.py
-- urls.py
-- views.py

Error 错误

[29/Jul/2019 13:09:45] "GET / HTTP/1.1" 200 12670
[29/Jul/2019 13:09:45] "GET /static_cdn/business/main.css HTTP/1.1" 404 77
[29/Jul/2019 13:09:45] "GET /static_cdn/business/scripts.js HTTP/1.1" 404 77

I also link to static files like {% static 'business/main.css' %} and I do have {% load static %} at the top of my document. 我还链接到{% static 'business/main.css' %}类的静态文件,并且在文档顶部确实有{% load static %}

Why isn't django able to find the static files? django为什么无法找到静态文件? The error says it is looking in the correct place but it is returning 404 error. 该错误表明它正在寻找正确的位置,但返回404错误。

If you need anything else, you can look at my code 如果您还有其他需要,可以看一下我的代码

Note: I think that django isn't able to read this files but I don't know why... Collectstatic puts all static_cdn/ into static/ in same app dir. 注意:我认为django无法读取此文件,但我不知道为什么... Collectstatic将所有static_cdn/放入相同应用程序目录中的static/中。

In your code, you have defined BASE_DIR as below 在代码中,您已经定义了BASE_DIR,如下所示

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

which means it is pointing to the root folder while your static_cdn folder is lying under business folder 这意味着当您的static_cdn文件夹位于企业文件夹下时,它指向根文件夹

While defining below, you will have to update path to one within business folder for STATIC_ROOT and STATICFILES_DIRS 在下面定义时,您将必须将业务文件夹中的路径更新为STATIC_ROOTSTATICFILES_DIRS

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn')
STATIC_URL = '/static_cdn/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static_cdn'),
]

as below 如下

STATIC_ROOT = os.path.join(BASE_DIR, 'business', 'static')
STATIC_URL = '/static_cdn/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'business', 'static_cdn'),
]

You probably have set DEBUG to False. 您可能已将DEBUG设置为False。 If and only if, you are in development environment , set DEBUG to True. 当且仅当您处于开发环境中时,才将DEBUG设置为True。 If you are planning for production, do collectstatic first and do appropriate changes in the settings.py and set DEBUG back to False. 如果您要进行生产计划,请首先进行静电收集,然后在settings.py中进行适当的更改,然后将DEBUG设置回False。 See the Django doc and your production server doc. 请参阅Django文档和您的生产服务器文档。 Also don't forget to restart the server. 同样不要忘记重启服务器。 Best wishes. 最好的祝愿。

Rename your static folder to static_cdn and update settings.py 将您的静态文件夹重命名为static_cdn并更新settings.py

settings.py settings.py

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn')
STATIC_URL = '/static_cdn/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static_cdn'),
]

ie this structure: 即这种结构:

project tree structure of app 应用程序的项目树结构

- business
-- migrations
--- ....
-- static_cdn
--- business
---- main.css
---- scripts.js
-- templates
--- business
---- base.html
---- home.html
-- templatetags
--- ...
-- __init__.py
-- admin.py
-- apps.py
-- models.py
-- tests.py
-- urls.py
-- views.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM