1

My site on django doesn't load static files

base.html:

{% load static %}
<link rel="stylesheet" href="{% static 'bootstrap/dist/css/bootstrap.css' %}">
<script src="{% static 'jquery/dist/jquery.min.js' %}"></script>
<script src="{% static 'bootstrap/dist/js/bootstrap.min.js' %}"></script>

files:

enter image description here

from terminal:

[13/Mar/2022 03:36:26] "GET / HTTP/1.1" 200 24975
[13/Mar/2022 03:36:26] "GET /static/bootstrap/dist/css/bootstrap.css HTTP/1.1" 404 1758
[13/Mar/2022 03:36:26] "GET /static/jquery/dist/jquery.min.js HTTP/1.1" 404 1751
[13/Mar/2022 03:36:26] "GET /static/bootstrap/dist/js/bootstrap.min.js HTTP/1.1" 404 1760
[13/Mar/2022 03:36:26] "GET /media/cache/77/1c/771c04f6935d264617dd3dec309a41d0.jpg HTTP/1.1" 404 1773

What could be be the reason of this?

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
maskalev
  • 360
  • 3
  • 9
  • Did you define **STATIC_URL** and **STATICFILES_DIRS** in settings? if not check this [link](https://docs.djangoproject.com/en/4.0/howto/static-files/) – Pranta chakraborty Mar 13 '22 at 06:35
  • yes. STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR / 'assets' ] STATIC_ROOT = BASE_DIR / 'static – maskalev Mar 13 '22 at 06:49
  • Please add more information like do you use runserver? Are you running it with debug=true? Content of urls.py? Your settings.py? – Razenstein Mar 13 '22 at 06:57
  • @Razenstein python3 manage.py collectstatic 436 static files copied to '/home/maskalev/Dev/hw05_final/static'. But I have 436 messages like this: Found another file with the destination path 'debug_toolbar/js/redirect.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path. But I'm sure that static directory was empty! No, debug=false. In case debug=true static is ok. Why? – maskalev Mar 13 '22 at 08:13
  • 3
    With debug= false django will not serve static files. That is intended behavior. See https://docs.djangoproject.com/en/4.0/howto/static-files/ – Razenstein Mar 13 '22 at 08:39

2 Answers2

0

Alxender, you should write these link and scripts in the base.html file and whenever you want static in a file write {% load static %} each time in each file. You should also run this code in the terminal before running the server

python manage.py collectstatic

You should also check if you have set these settings in project/settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = (str(BASE_DIR.joinpath('static')),)
STATIC_ROOT = str(BASE_DIR.joinpath('staticfiles')) 
STATICFILES_FINDERS = [
 "django.contrib.staticfiles.finders.FileSystemFinder",
 "django.contrib.staticfiles.finders.AppDirectoriesFinder",
   ]
Vijay Soni
  • 197
  • 1
  • 10
  • vijay, i wrote html code from base.html (now i checked it on my question). and i ran collectstatic of course. – maskalev Mar 13 '22 at 06:47
0

From the comments:

With debug= false django will not serve static files. That is intended behavior. > See docs.djangoproject.com/en/4.0/howto/static-files

Thanks to @Razenstein for answer!

ouflak
  • 2,458
  • 10
  • 44
  • 49
maskalev
  • 360
  • 3
  • 9