To server user uploaded files, see [[Sever media files uploaded by a user during development]].
- In
settings.py
we already have aSTATIC_URL = 'static/'
- We create a folder
static/images/
under the project’s folder such asmoviereviews
. - We store fixed images such as
logo.png
in this folder. - In template files such as the
moviereviews/templates/base.html
, we first load thestatic
function with{% load static %}
at the top of the template file and then reference the static file as{% static 'images/logo.png' %}
, Django will iterates over apps to find the file that is stored instatic/images
.
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>Movies App</title>
</head>
<body>
<img src="{% static 'images/logo.svg' %}" />
</body>
</html>
- Since the image is not attached to a specific app(it is attached to the project folder), we need to include the
moviereivews/static
folder in the settings.py:
STATICFILES_DIRS = [
BASE_DIR / 'moviereviews/static/'
]
See: https://docs.djangoproject.com/en/4.2/howto/static-files/