Search code examples
pythondjangostatic-files

Django:How to setup static files in django project


I created a simple project in Django but static files(CSS) not working.

settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.portfolio),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

HTML file

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="{% static 'portfolio.css' %}">
</head>
<body>
    <h1>hello world</h1>
</body>
</html>

picture of the project directory

picture of the project directory.

blog is app and my_site is a project.


Solution

  • try adding

    os.path.join(BASE_DIR, "my_site/static")
    

    to STATICFILES_DIRS

    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static"),
        os.path.join(BASE_DIR, "my_site/static")
    ]
    

    OR

    place the static folder in the root my_site folder