Django Static Files: CSS, JavaScript, jQuery and Bootstrap

Recommended preparation: set up a Django project and read the Django tutorial overview before trying the code.

Django serves local CSS, JavaScript and images through its static-file system during development. Use {% load static %} and {% static "..." %} instead of hard-coding file paths in templates.

templates/register.html — load a local CSS file

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="{% static 'css/my_style.css' %}">
</head>
<body><h1 class="my_h2">Welcome</h1></body>
</html>

A registration page can load its own CSS using Django’s static tag. The file must exist in a discoverable static directory; Django does not search arbitrary folders by URL alone.

On this page — Show page sectionsHide page sections

Create a new project and app

Terminal — demonstration project

django-admin startproject jq_project
cd jq_project
python manage.py startapp pages
python manage.py runserver

App namespaced static files help avoid name conflicts

jq_project/
├── manage.py
├── jq_project/settings.py
├── jq_project/urls.py
└── pages/
    ├── views.py
    ├── urls.py
    ├── static/pages/css/site.css
    ├── static/pages/js/site.js
    └── templates/pages/register.html

Register the app and configure a view

settings.py — staticfiles is enabled by default

# jq_project/settings.py — add to the existing list
INSTALLED_APPS = [
    "pages",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]
STATIC_URL = "static/"

pages/views.py

from django.shortcuts import render

def register(request):
    return render(request, "pages/register.html")

pages/urls.py

from django.urls import path
from . import views

urlpatterns = [path("register/", views.register, name="register")]

jq_project/urls.py

from django.urls import include, path
from django.contrib import admin

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("pages.urls")),
]

Namespaced CSS and JavaScript

Include files from pages/static/pages/

{% load static %}
<link rel="stylesheet" href="{% static 'pages/css/site.css' %}">
<script src="{% static 'pages/js/site.js' %}" defer></script>

pages/static/pages/css/site.css

.my_h2 { color: navy; font-size: 1.5rem; }

pages/static/pages/js/site.js — vanilla JavaScript option

document.addEventListener("DOMContentLoaded", () => {
  const button = document.getElementById("b1");
  if (button) button.addEventListener("click", () => alert("Welcome"));
});

Add jQuery with a local file

You can download jQuery into static/js/ and bind a click handler. If jQuery is needed for an existing plugin, download the supported release from the official project, store it in your static directory, and load it before dependent scripts. For simple click handlers, vanilla JavaScript often avoids an extra dependency. See jQuery click events.

Template — local jQuery file must be present

{% load static %}
<button type="button" id="b1">Click me</button>
<script src="{% static 'pages/js/jquery.min.js' %}"></script>
<script>
  $(function () {
    $("#b1").on("click", function () { alert("Button clicked"); });
  });
</script>

Include Bootstrap styling

Local Bootstrap CSS

{% load static %}
<link rel="stylesheet" href="{% static 'pages/css/bootstrap.min.css' %}">
<button type="button" class="btn btn-danger" id="b1">Click me</button>

When using a recent Bootstrap release, do not assume it requires jQuery. Bootstrap 5 JavaScript is independent of jQuery. For older Bootstrap 4 pages, follow the appropriate version-specific dependency instructions. Related examples: Bootstrap introduction, jQuery and buttons, and jQuery lessons.

Development versus production static files

Illustrative settings and management command

# settings.py — choose a directory for collected production assets
STATIC_ROOT = BASE_DIR / "staticfiles"

# Run before a production deployment
python manage.py collectstatic

collectstatic copies static assets into STATIC_ROOT for serving through your production static-file setup; Django’s development server should not be used as a production asset server. Set an appropriate STATIC_ROOT outside source app directories.

Troubleshooting missing styles or scripts

  • 404 for /static/…: check the file path, app registration, static tag and app namespacing.
  • Invalid block tag static: include {% load static %} in the template.
  • Styles work locally but not online: configure STATIC_ROOT, run collectstatic and arrange production file serving.
  • jQuery is not defined: load the jQuery library before code that calls $().
  • Conflicting Bootstrap/jQuery versions: use documented dependencies for the exact major version.

Additional asset loading examples

Responsive image with a stable asset path

{% load static %}
<img src="{% static 'pages/images/logo.png' %}" alt="Example site logo" width="200" height="60">

Vanilla JavaScript class toggling

document.querySelectorAll(".js-toggle").forEach((button) => {
  button.addEventListener("click", () => {
    button.classList.toggle("active");
  });
});

Equivalent jQuery interaction when jQuery is already in use

$(function () {
  $(".js-toggle").on("click", function () {
    $(this).toggleClass("active");
  });
});

Order dependent JavaScript correctly

{% load static %}
<script src="{% static 'pages/js/jquery.min.js' %}"></script>
<script src="{% static 'pages/js/site.js' %}" defer></script>

When loading a locally hosted jQuery script and a deferred dependent script, the normal parser-blocking jQuery asset will execute first. In performance-sensitive production pages you can use an appropriately tested deferred loading strategy for both files.

Find the physical location Django resolves for a static asset

python manage.py findstatic pages/css/site.css

Add a global assets directory without replacing app-level static files

# settings.py — optional extra project-level static folder
STATICFILES_DIRS = [BASE_DIR / "assets"]

Frequently asked questions

Should I run collectstatic during development?

Usually not. With the staticfiles app enabled, Django’s development server discovers assets for you.

Can I use a content delivery network for Bootstrap?

Yes, but check version integrity, privacy and performance requirements. For full control, host a supported release yourself.

Why use namespaced static directories?

Two Django apps may contain a styles.css filename. App-specific prefixes such as pages/css/site.css prevent collisions.

Previous: Template for loops

Next: MySQL connection and models

Browse the complete Django tutorial series.

Additional references

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer