Django is not able to find static files

Viewed 567

I've started a new Django project and run into issue at the very begining.

I have created a "core" app and inside I have prepared a simple html page using bootstrap. Instead of using CDN I have downloaded bootstrap files and put it under static directory. The problem is Django can't find those static files.

I am using the latest version of Django

>>> django.VERSION
(3, 2, 5, 'final', 0)

Snippet from my base.html file:

{% load static %}
<!-- Bootstrap core CSS -->
<link href="{% static 'assets/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="{% static 'headers.css' %}" rel="stylesheet">

settings.py

STATIC_URL = '/static/'
STATICFILES_DIR = [
    BASE_DIR / 'static'
]

and my directory structure:

.
├── apps
│   └── core
│       ├── __init__.py
│       ├── admin.py
│       ├── apps.py
│       ├── migrations
│       │   ├── __init__.py
│       ├── models.py
│       ├── templates
│       │   └── core
│       │       ├── base.html
│       │       └── index.html
│       ├── tests.py
│       └── views.py
├── db.sqlite3
├── manage.py
├── ref_manager
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── static
│   ├── assets
│   │   └── bootstrap
│   │       ├── css
|   |       |   ...
│   │       │   ├── bootstrap.css
│   │       │   ├── bootstrap.css.map
│   │       │   ├── bootstrap.min.css
│   │       │   ├── bootstrap.min.css.map
│   │       └── js
│   │           ├── bootstrap.bundle.js
│   │           ├── bootstrap.bundle.js.map
│   │           ├── bootstrap.bundle.min.js
│   │           ├── bootstrap.bundle.min.js.map
│   │           ├── bootstrap.esm.js
│   │           ├── bootstrap.esm.js.map
│   │           ├── bootstrap.esm.min.js
│   │           ├── bootstrap.esm.min.js.map
│   │           ├── bootstrap.js
│   │           ├── bootstrap.js.map
│   │           ├── bootstrap.min.js
│   │           └── bootstrap.min.js.map
│   └── headers.css
4 Answers

For your development environment, in your urls.py:

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

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

You need staticfiles application in your settings:

INSTALLED_APPS = [
    ...
    'django.contrib.staticfiles',
    ...
]

Set STATIC_URL, and STATICFILES_DIRS if you have static folder outside app directories.

This work fine in development mode:

python manage.py runserver

In operational environment, static files are copied into STATIC_ROOT by:

manage.py collectstatics

Then you can add to your urlpatterns

static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Static files will be served by django, but it is better to serve static files directly with your web server.

Try it in your settings.py file:

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

Please fix indentation error, if any

In settings.py file change below code -

   STATICFILES_DIRS = [
       (BASE_DIR / "static"),
   ]

This works for me.

Related