djangodjango-static

django static files 404 error after migration


static folder path correct and it was all working before migration.

{%load staticfiles%}
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="{% static "css/mycss.css"%}"/>
        <title>My first Django App</title>
    </head>
    <body>
    <h1>{{somthin}}</h1>
    <img src="{% static 'images/zoro.jpg'%}" alt="Oops!No Image">

    </body>
</html>

after python manage.py runserver in terminal static files are showing 404 error.


Solution

  • make sure all these configuration you have in your settings.py

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    STATIC_URL = '/static/'
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static"),
    ]
    
    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
    
    MEDIA_URL = "/media/"
    MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
    

    and in main urls.py

    if settings.DEBUG:
        urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    and use {% load static %} tag in templates, also if you use double quote outside curly brackets, use single quote inside

    <link rel="stylesheet" href="{% static 'css/mycss.css' %}"/>