Home > OS >  Django doesn't see static files (404 error)
Django doesn't see static files (404 error)

Time:10-06

I was faced with a problem when django can't see static files and I'm getting 404 error every time visiting the page.

[05/Oct/2021 19:25:07] "GET /static/main/css/index.css HTTP/1.1" 404 1813

Here is my setting.py


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/


STATIC_URL = '/static/'

STATICFILES_DIRS = ( os.path.join('static'), )

# Default primary key field type

Another part was cut

HTML file

{% load static %}
<!DOCTYPE html>


<html>
<head>
    <meta charset="utf-8" />
    <title>Hello World</title>
    <link rel="stylesheet"  href="{% static 'main/css/index.css' %}" />
</head>

Another part was cut too

My files (png)

CodePudding user response:

add this in your application's urls.py

url(r'^media/(?P<path>.*)/$', serve, {'document_root': settings.MEDIA_ROOT}, name = 'media'),

do not forget to import this:

from django.conf import settings
from django.conf.urls import url
from django.views.static import serve

your settings should be like this:

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

STATIC_URL = '/media/static/'
STATICFILES_DIRS = (BASE_DIR / 'media/static', ) 

STATIC_ROOT = 'media/staticfiles/'

CodePudding user response:

add this in you main urls.py

from django.conf.urls import url
from django.views.static import serve


from django.conf.urls.static import static
from django.conf import settings

    url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
    url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
if settings.DEBUG:
    urlpatterns  = static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
    urlpatterns  = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

and in you settings.py

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES_DIR = BASE_DIR / 'templates'
STATIC_DIR = BASE_DIR / 'static'

and in the bottom of you settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    STATIC_DIR,
]

STATIC_ROOT = STATIC_CDN

this has to work for you and tell me if still the issue is not solve and update your question what you tried

  • Related