【问题标题】:Why 404 error occurs even though I have set STATIC_ROOT and urls.py为什么即使我设置了 STATIC_ROOT 和 urls.py 也会出现 404 错误
【发布时间】:2021-12-02 01:29:51
【问题描述】:

我不知道为什么,但在尝试使用像这样的静态文件时出现 404 错误:{% static 'js/some.js' %}

这是我的 urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path("account/", include("account.urls")),
    path("", include("post.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

这是我的 settings.py

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static/'

# media config 
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR  / 'media/'

谢谢!!

编辑

这是我得到的错误

GET http://127.0.0.1:8000/static/js/some.js net::ERR_ABORTED 404 (Not Found)

而且你在这里看到的网址是对的。

【问题讨论】:

    标签: python django django-staticfiles django-settings


    【解决方案1】:

    请试试这个:

    # settings.py
    STATIC_URL = '/static/'
    STATICFILES_DIRS = [
        BASE_DIR / "static",
    ]
    

    【讨论】:

      猜你喜欢
      • 2015-03-08
      • 2016-03-25
      • 2014-06-22
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2021-11-25
      • 1970-01-01
      相关资源
      最近更新 更多