Search code examples
pythondjangodjango-staticfilesdjango-settings

Why 404 error occurs even though I have set STATIC_ROOT and urls.py


I don't know why, but I get 404 errors when trying to use static file like this: {% static 'js/some.js' %}.

Here's my 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)

And this is my settings.py

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

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

Thanks!!

EDIT

Here's the error I get

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

And the url you can see here is right.


Solution

  • Please try this:

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