101👍
This worked for me
If django >= 1.11
{% load static %}
If django < 1.11
{% load staticfiles %}
- [Django]-Changing a project name in django
- [Django]-Threaded Django task doesn't automatically handle transactions or db connections?
- [Django]-Nginx uwsgi (104: Connection reset by peer) while reading response header from upstream
70👍
include
{% load static %}
just above tag in your html file, it will make HTML file to load static files like css, js, images etc
- [Django]-How to access custom HTTP request headers on Django Rest Framework?
- [Django]-Django: guidelines for speeding up template rendering performance
- [Django]-Proper way to consume data from RESTFUL API in django
- [Django]-Django-Admin: CharField as TextArea
- [Django]-Inline in ModelForm
- [Django]-Paginating the results of a Django forms POST request
9👍
07 Sept, 2020
i also faced the same problem on Django 3.2.2
and this worked for me.
{% load static %}
- [Django]-How to make the foreign key field optional in Django model?
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
- [Django]-How do I create a login API using Django Rest Framework?
5👍
{% load static %}
or
{% load staticfiles %}
both will work.
Just be sure you use equal amount of spaces between opening and closing of ‘{‘ and ‘%’.
- [Django]-Cleanest & Fastest server setup for Django
- [Django]-Reference list item by index within Django template?
- [Django]-Django template and the locals trick
- [Django]-How to run celery as a daemon in production?
- [Django]-What is the best django model field to use to represent a US dollar amount?
- [Django]-Include intermediary (through model) in responses in Django Rest Framework
2👍
Hint: be sure that you use {% load static %} in all templates your using to extend static files. Extending a base.html file doesn’t carry over {% load static %} for you.
- [Django]-Django: Record with max element
- [Django]-Access web server on VirtualBox/Vagrant machine from host browser?
- [Django]-Django select_for_update cannot be used outside of a transaction
1👍
It’s OK now. I resolved it myself.
I’m sorry. I confused two similar html files. The one I put on here (header.html) was right but header_authenticated.html was wrong.
- [Django]-Coverage.py warning: No data was collected. (no-data-collected)
- [Django]-Django Background Task
- [Django]-How to iterate through dictionary in a dictionary in django template?
0👍
For a folder structure like this
myproject/
manage.py
myapp/
__init__.py
models.py
views.py
static/
style.css
templates/
base.html
index.html
In your settings.py
, make sure you have
django.contrib.staticfiles
in theINSTALLED_APPS
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# Application definition
INSTALLED_APPS = [
...
'django.contrib.staticfiles',
...
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
In your base.html
template, make sure to have {% load static %}
tag, like
<html>
<head>
<title>{% block title %}Add App Title here{% endblock %}</title>
{% load static %}
<link rel="stylesheet" href="{% static 'style.css' %}">
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Then, you’ll want to run python manage.py collectstatic
which will generate a staticfiles/
folder in the root of myproject/
(where manage.py
is). That’s it!
If you use Docker, then make sure to include in your Dockerfile the following step
RUN python manage.py collectstatic --noinput
If you will to know more about to serve static files in production, consider this answer.
- [Django]-Get all table names in a Django app
- [Django]-Django on IronPython
- [Django]-Problems extend change_form.html in django admin