Chapter 4: Django – Global Static Files

Global static files — the CSS, JavaScript, images, favicons, fonts, etc. that should appear on every single page of your app.

Most beginners start by putting everything inside one app’s static/ folder (e.g. polls/static/polls/css/style.css) — which works… until you add a second app, or want a site-wide footer logo, or a global navbar style, or a favicon that works everywhere.

Today we’re going to fix that forever — step by step — like I’m sitting next to you, creating folders, writing files, editing settings, and testing together.

We will make your whole site (not just /polls/) look beautiful, consistent, and modern using project-level global static files.

Step 1 – Understand the Two Kinds of Static Files

Type Location example Purpose / When to use Served from (dev) Served from (prod)
App-specific polls/static/polls/css/polls.css Styles / JS / images only relevant to polls pages app’s static/ folder collectstatic → STATIC_ROOT
Global / Project-level static/css/global.css Site-wide: base layout, fonts, navbar, footer, logo, favicon STATICFILES_DIRS collectstatic → STATIC_ROOT

Golden rule 2026:

  • Use app-specific for things that only make sense in one app (polls/static/polls/css/vote-button.css)
  • Use global/project-level for everything that appears on every page (static/css/base-layout.css, static/images/logo.png, static/js/site-analytics.js)

Step 2 – Create the Global Static Folder (If Not Already Done)

In project root (next to manage.py):

Bash

Now your structure looks like:

text

Step 3 – Tell Django Where to Find Global Static Files

Open mysite/settings.py

Add or update:

Python

Important — STATICFILES_DIRS is the key line that makes Django search your static/ folder.

Step 4 – Create Real Global Static Files

1. Global CSS – static/css/global.css

CSS

2. Global JavaScript – static/js/site.js

JavaScript

3. Logo & Favicon – static/images/logo.png and static/icons/favicon.ico

(You can download any small logo PNG and favicon ICO or use placeholder tools online)

Step 5 – Link Everything in base.html

In templates/base.html:

HTML

Step 6 – Test It All

  1. Save everything
  2. Restart server if needed: python manage.py runserver
  3. Open browser → visit /polls/ or /
  4. Check:
    • Logo appears in header
    • Global styles applied (background, fonts, button look)
    • Favicon in browser tab
    • Console → “Global site.js loaded…” message
    • Dev tools → Network tab → all static files 200 OK

If 404:

  • Forgot {% load static %} → add at top of template
  • Wrong path → check exactly static/css/global.css (no typos)
  • Server not restarted after adding files

Step 7 – Production (When You Deploy)

Run once before deploy:

Bash

→ All files copied to staticfiles/

Then:

  • DEBUG = False
  • ALLOWED_HOSTS correct
  • WhiteNoise middleware + storage set (see previous lesson)
  • Deploy → static files served automatically

Your Quick Task Right Now

  1. Create static/css/global.css with the code above
  2. Add {% load static %} + CSS link + logo to base.html
  3. Restart server → check if header looks nicer
  4. Add a small JS file → include it → see console message
  5. (Optional) Add favicon → refresh → see browser tab icon

Tell me what feels next:

  • “Done! Now show me how to use Bootstrap 5 / Tailwind locally or CDN”
  • “How to organize fonts / custom icons / favicons properly?”
  • “What happens if I have conflicting filenames (global vs app-specific)?”
  • “Got 404 / style not applying – here’s error/screenshot”
  • Or finally ready for Django Forms + Voting + POST + F() + results page

You now have global static files working beautifully — your entire site now shares one consistent look.

You’re doing really well — let’s keep building! 🚀🇮🇳

You may also like...

Leave a Reply

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