Chapter 1: Django – Add Static File

Add Static File: How to properly add and use static files (CSS, JavaScript, images, fonts, favicons, etc.) in Django.

Many beginners either:

  • keep writing everything inline inside <style> or <script> tags → code becomes unmaintainable mess
  • put static files in wrong folders → get 404 errors forever
  • don’t understand the difference between development (runserver) and production (collectstatic) → panic during deployment

Today we are going to fix all of that — step by step — like I’m sitting next to you, creating folders, writing files, running commands, and testing together on your laptop.

We will make your polls app look much more professional with global CSS, app-specific styles, a logo image, and a tiny JavaScript alert — all properly organized.

Goal for Today

By the end you will have:

  • Clean static/ folder structure (global + app-specific)
  • Global CSS linked from base.html
  • App-specific CSS for polls pages
  • A logo image in the header
  • A small JavaScript file that runs on page load
  • Everything working in development
  • Understanding of how to prepare for production (collectstatic)

Step 1: Understand the Two Worlds of Static Files

Mode Command Where Django looks for files How files are served When used
Development python manage.py runserver STATICFILES_DIRS + each app’s static/ subfolder Django’s dev server serves them automatically Everyday development
Production Real web server (nginx, gunicorn + Whitenoise, etc.) One single folder created by collectstatic Web server serves them directly (very fast) When deploying (Railway, Render, Heroku, VPS…)

Golden rule: Develop with the same folder structure you will use in production — never rely on dev-server magic.

Step 2: Create the Standard Static Folder Structure

In your project root (next to manage.py):

Bash

Now create app-specific static folder (recommended best practice):

Bash

Why the extra polls/ folder inside static/?

Namespacing — prevents name conflicts If later you add a blog app that also has style.css, Django knows which one to use.

Final structure should look like:

text

Step 3: Configure settings.py (Most Important Step)

Open mysite/settings.py

Add or update these lines:

Python

Important: STATICFILES_DIRS tells Django where to find extra static files beyond app folders. STATIC_ROOT is only used in production — never put files there manually.

Step 4: Create Some Real Static Files

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

CSS

2. App-specific CSS – polls/static/polls/css/polls.css

CSS

3. A small JavaScript file – static/js/alert.js

JavaScript

4. An image (logo) – static/images/logo.png

(You can download any small logo PNG or create a placeholder text image)

Step 5: Load & Use Static Files in Templates

In your master template templates/base.html (add at top):

HTML

Now link files:

HTML

Step 6: Test in Development

  1. Save all files
  2. Restart server if needed (python manage.py runserver)
  3. Open any page (e.g. /polls/ or /)
  4. Check browser dev tools (F12 → Network tab):
    • See global.css, polls.css, logo.png, alert.js load with 200 OK
    • See console message “Static JavaScript loaded successfully!”
  5. See styles applied (font, colors, card shadows, logo)

Common 404 errors & fixes

  • Forgot {% load static %} → 404 on {% static … %}
  • Wrong path → check folder structure exactly (polls/static/polls/css/…)
  • File not saved or server not restarted → Ctrl+F5 hard refresh

Step 7: Production Preparation (collectstatic)

When you deploy (Railway, Render, Heroku, VPS…):

Bash

→ Django copies all static files from:

  • STATICFILES_DIRS (project static/)
  • every app’s static/ folder

into STATIC_ROOT (staticfiles/ folder)

Then configure your web server to serve /static/ from staticfiles/.

Your Quick Practice Task (Do This Right Now)

  1. Create folders and files exactly as shown
  2. Add {% load static %} to base.html
  3. Link global CSS + logo image + JS file
  4. Create polls/static/polls/css/polls.css with some style (e.g. .poll-card { border: 2px solid green; })
  5. In polls/templates/polls/index.html → add class poll-card to one div → see style apply
  6. Check browser console → see JS message

Tell me what feels next:

  • “Done! Now show me how to use Tailwind or Bootstrap via CDN / local files”
  • “How to handle user-uploaded images (media files) later?”
  • “What is Whitenoise? How to use it for production static files?”
  • “Got 404 on static file – here’s the URL and error”
  • Or finally ready for Django Forms + Voting + POST + F() increment?

You now have a clean, professional, scalable static files setup — your site is starting to look like a real 2026 web app.

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

You may also like...

Leave a Reply

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