Python Django Static not showing

Viewed 20

I tried many solutions available in stock overflow too but non of it solved my problems.Django is my very first framework right after learning HTML,CSS,python(basics) I don't know what I am doing wrong please help me fix it here is my code Here is my folder structure:

[folder structure][1]

Here is my settings,URL,HTML code

   STATICFILES_DIR=[
   os.path.join(BASE_DIR,"static")
   #BASE_DIR/'static'
   ]




   from django.urls import path
   from . import views
   from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
   urlpatterns=[
   path('', views.home, name='home'),
   path('customer',views.customer,name='customer'),
   path('products',views.products,name='products')
   ]
   urlpatterns += staticfiles_urlpatterns()


   {% load static %}
   <!DOCTYPE HTML>
   <html lang="en">
   <head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Dash Board</title>
   <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" 
rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
   <link type="text/css" rel="stylesheet" href="{% static 'css/main.css' %}">
 
   </head>
   <body>
   {% include "navbar.html" %}
   {% block content%}

   {% endblock %}
   <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
  </body>

  </html>```


  [1]: https://i.stack.imgur.com/65Z9x.png
1 Answers

Your static folders should be in the app folder, not in the general templates folder, then Django cant find them. In development Django will search each app for its static files.

I would also recommend grouping your templates in the app itself, that makes it easier to work with later on when there are a lot of templates.

When deploying, you can run python manage.py collectstatic to collect them in the defined path from settings.py.

Structure example:

.
├── ...
├── crml
│   ├── templates
│   ├── static          # here 
│   ├── urls.py
│   └── views.py
└── ...

Related