๐ Introduction
In Django, static files include:
- CSS
- JavaScript
- Images
๐ These files are stored separately and loaded using {% static %}.
๐ฏ Program Statement
๐ Configure static files and apply external CSS to a template.
๐ง Why Static Files Are Important
- Styling (CSS)
- Interactivity (JS)
- UI Design
โ๏ธ Step 1: Configure Static Settings
๐ File: settings.py
๐น Path:
myproject/myproject/settings.py
๐น Code:
import os
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
๐ง Explanation
STATIC_URLโ URL prefixSTATICFILES_DIRSโ location of static folder
โ๏ธ Step 2: Create Static Folder Structure
๐ Project Structure:
myproject/
static/
css/
style.css
โ๏ธ Step 3: Create CSS File
๐ File: style.css
๐น Path:
myproject/static/css/style.css
๐น Code:
body {
background-color: #f0f8ff;
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
color: darkblue;
}
p {
color: #333;
font-size: 18px;
}
โ๏ธ Step 4: Create Template
๐ File: home.html
๐น Path:
myproject/templates/home.html
๐น Code:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>Styled Page</title>
<!-- Link External CSS -->
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<h1>Welcome to Django</h1>
<p>This page is styled using external CSS.</p>
</body>
</html>
โ๏ธ Step 5: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
โ๏ธ Step 6: URL Mapping
๐ File: urls.py
๐น Path:
myproject/myproject/urls.py
๐น Code:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
]
โ๏ธ Step 7: Run Server
python manage.py runserver
๐ Step 8: Output
โ Output Features:
- Light background color
- Styled heading
- Centered text
๐ง How It Works
- Django looks for static files in
static/folder {% load static %}enables static tag{% static 'css/style.css' %}loads CSS- Browser applies styling
๐ฅ Key Concepts
Load Static
{% load static %}
Link CSS
<link rel="stylesheet" href="{% static 'css/style.css' %}">
Static Folder Path
static/css/style.css
โ ๏ธ Common Errors (VERY IMPORTANT ๐ฅ)
โ Forgot {% load static %}
๐ CSS will not load
โ Wrong path
โ Wrong:
{% static 'style.css' %}
โ Correct:
{% static 'css/style.css' %}
โ STATICFILES_DIRS missing
๐ Static folder not detected
โ CSS not updating
๐ Do:
Ctrl + Shift + R (Hard refresh)
๐งช Practice Questions
- Add multiple CSS files
- Change font and colors
- Create separate CSS for header
- Add background image
๐ค Viva Questions & Answers
1. What are static files in Django?
Static files are files like CSS, JavaScript, and images used for styling and UI.
2. What is STATIC_URL?
It defines the URL prefix for static files.
3. What is STATICFILES_DIRS?
It tells Django where static files are stored.
4. Why use {% load static %}?
To enable static tag inside templates.
5. How to link CSS file?
Using <link> tag with {% static %}.
6. What happens if static not configured?
CSS, JS, images will not load.
7. What is external CSS?
CSS written in a separate file and linked to HTML.
8. Why use external CSS?
It improves maintainability and reuse.
9. Where should static folder be placed?
At project level or inside app.
10. Why is static configuration important?
It ensures proper loading of UI resources.
๐ Navigation
๐ Next Post: Display Image using Static Tag
๐ Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
