How do we manage a static file to a template in Django?

In This blog, we are going to learn, How do we manage a static file to a template in Django?

What are static files?

Static files are files that don’t change when your application is running.

Static files are files that clients download as they are from the server.

Static Files such as Images, CSS or JS files are often loaded via a different app in production websites to avoid loading multiple stuff from the same server.

Django provides django.contrib.staticfiles to help you manage them.

To configure static files

  1. Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.

now runserver once to confirm everything is working correctly.

python manage.py runserver

2. In your settings file, define STATIC_URL, for example:

STATIC_URL = 'static/'

to Load and use Static Files in Django

3. In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE.

{% load static %}
<img src="{% static 'sample/example.jpg' %}" alt="MyPic"

Store your static files in a folder called static in your app. For example my_sample/static/sample/example.jpg

Thanks for following and Reading this blog.

How do we manage a static file to a template in Django?

dexpoundsofttechsolution