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 prefix
  • STATICFILES_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

πŸ‘‰ http://127.0.0.1:8000/


βœ… Output Features:

  • Light background color
  • Styled heading
  • Centered text

🧠 How It Works

  1. Django looks for static files in static/ folder
  2. {% load static %} enables static tag
  3. {% static 'css/style.css' %} loads CSS
  4. 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

  1. Add multiple CSS files
  2. Change font and colors
  3. Create separate CSS for header
  4. 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

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

Leave a Reply

Your email address will not be published. Required fields are marked *