Django - 即使在手动更正路径后也找不到我的静态文件

Django - cant find my static files even after manually correcting the path

我遇到了一个问题,即使经过一个晚上的尝试,我仍无法解决。

对于我的项目,我只有一个 static 目录,并且我在其中包含了 'login' 页面的 css 文件。然而,当我尝试加载网页时,出现 404 错误。

当我尝试使用 'findstatic' 或 'collectstatic' 命令查找文件时,文件被跳过。它就像 'static' 目录对 django 是不可见的。我什至直接将 'Login' 文件夹复制到 'static_root',但即便如此,程序仍无法找到 Django 文件。 有谁知道我做错了什么?

我的项目文件夹如下所示:

* MPS (main folder)  
      * Scraper (app)
      * Database (app)
      * Templates
      * Static_root
      * Static
          * Login
                * css
                     * style.css

我的settings.py配置如下:

STATIC_URL = 'static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root/')

然后我用下面的代码调用模板中的静态文件:

{% load staticfiles %}
(...)
<link rel="stylesheet" href="{% static 'login/css/style.css' %}">

您的项目文件夹结构看起来与大多数 Django 项目有点不同。传统上它看起来更像这样:

myproject/
    urls.py
    wsgi.py
    settings.py
myapp/
    static/
        myapp/
            css/
                style.css
            js/
                index.js
manage.py

请注意,myapp 的应用程序静态文件位于与应用程序文件夹同名的子文件夹中。这是为了轻松地为文件命名空间,您可以在 docs 中阅读更多相关信息。

Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.