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.
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
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")),
]
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"));
});
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>
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.
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.
static tag and app namespacing.{% load static %} in the template.STATIC_ROOT, run collectstatic and arrange production file serving.$().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"]
Usually not. With the staticfiles app enabled, Django’s development server discovers assets for you.
Yes, but check version integrity, privacy and performance requirements. For full control, host a supported release yourself.
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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.