Django

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

You may also like...

Leave a Reply

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