In Django, static files are those files that are not dynamically generated by the application; they include CSS, JavaScript, and images. Properly managing static files is crucial for ensuring your web application looks and behaves as intended. This guide will walk you through three practical examples of using static files in Django.
When building a web application, styling is essential to make it visually appealing. This example demonstrates how to include CSS files in your Django project.
First, create a directory named static
in your app folder, and inside it, create another folder named css
. This is where you will store your CSS files.
Create your CSS file: myapp/static/css/style.css
and add some basic styles:
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
h1 {
color: #333;
}
In your Django template, load the static files and link your CSS:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<title>My Django App</title>
</head>
<body>
<h1>Welcome to My Django App!</h1>
</body>
</html>
django.contrib.staticfiles
is included in your INSTALLED_APPS
in settings.py
.collectstatic
command to gather all static files into a single directory for production.JavaScript is vital for adding interactivity to your web pages. This example shows how to include a JavaScript file in your Django project.
Create a new folder named js
inside the static
directory, and place your JavaScript file there.
Create your JavaScript file: myapp/static/js/script.js
with a simple alert:
document.addEventListener('DOMContentLoaded', function() {
alert('Welcome to My Django App!');
});
Include the JavaScript file in your 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">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<script src="{% static 'js/script.js' %}"></script>
<title>My Django App</title>
</head>
<body>
<h1>Welcome to My Django App!</h1>
</body>
</html>
defer
attribute in the <script>
tag if you want to load the script after the HTML has been parsed.Images are integral to many web applications, whether for branding or content. This example shows how to serve images using Django static files.
Create an images
folder inside your static directory to hold your images.
myapp/static/images/
directory (e.g., logo.png
).Reference the image in your 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">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<title>My Django App</title>
</head>
<body>
<h1>Welcome to My Django App!</h1>
<img src="{% static 'images/logo.png' %}" alt="Logo">
</body>
</html>
These examples of Django static files example will help you effectively manage and utilize static resources in your web applications!