Static files like images, CSS, and JavaScript make your web pages look good and work well. Django helps you include these files easily in your templates.
Static files in templates in Django
{% load static %}
<img src="{% static 'path/to/file.jpg' %}" alt="description">
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
<script src="{% static 'js/script.js' %}"></script>Always start your template with {% load static %} to use the static tag.
The {% static %} tag helps build the correct URL for your static files.
images folder inside your static files.{% load static %}
<img src="{% static 'images/logo.png' %}" alt="Site Logo">{% load static %}
<link rel="stylesheet" href="{% static 'css/main.css' %}">{% load static %}
<script src="{% static 'js/app.js' %}"></script>This template loads static files: a CSS file for styles, an image for the logo, and a JavaScript file for behavior. It shows how to organize and include static files in a Django template.
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Static Files Example</title>
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
<body>
<header>
<img src="{% static 'images/logo.png' %}" alt="Site Logo">
<h1>Welcome to My Site</h1>
</header>
<script src="{% static 'js/script.js' %}"></script>
</body>
</html>Make sure your static files are placed in the correct folders inside your Django app or project.
During development, Django serves static files automatically, but in production, you need to configure your web server to serve them.
Use the {% static %} tag instead of hardcoding URLs to keep your code flexible.
Use {% load static %} at the top of your template to enable static file usage.
Use {% static 'path/to/file' %} to get the correct URL for static files.
Static files include images, CSS, and JavaScript that improve your website's look and behavior.