Configure Static Files and Apply External CSS to a Template
π 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
