nicksheen nicksheen - 1 month ago 7
Python Question

How to load css as static in Django

Hi I have the following structure in my project:

myapp
├── app
│   ├── app
| | ├── settings.py
│ │ ├── [...]
│   ├── templates
│   │   ├── base.html
│   │   ├── my_test.html
│   └── app2
│   ├── [...]
├── statics
│   ├── bootstrap
│   │   ├── css
│   │   │   ├── bootstrap.min.css
│   └── static_root


and I want to use the bootstrap.min.css in the base.html (which looks currently like this):

{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
{% block head %}
<link rel="stylesheet" type="text/css" href="../../statics/bootstrap/css/bootstrap.min.css">
{% endblock head %}
</head>
<body>
{% block content %}{% endblock content %}
</body>
</html>


and in my settings.py:

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'statics', 'static_root')

STATICFILES_DIR = {
os.path.join(BASE_DIR, 'statics', 'bootstrap'),
}


so how I have to chance it that it works because in this version it does not work (
Page not found at /statics/bootstrap/css/bootstrap.min.css
).

Answer Source

Obviously this (Page not found at /statics/bootstrap/css/bootstrap.min.css). won't work. Your static_url is /static. Try to check on /static/bootstrap/css/bootstrap.min.css

STATIC_URL = '/static/'  

Also I recommend using static template tag

{% static 'bootstrap/css/bootstrap.min.css' %}