Chapter 5: Add CSS File to the Project

Add CSS File to the Project: Many beginners keep writing styles directly inside <style> tags in base.html or in individual templates — which works for 5 minutes, but becomes a complete nightmare when you want to change colors, fonts, button shapes or spacing later.

Today we are going to do it the right way from the very beginning: one clean global CSS file + optional app-specific CSS, properly linked, working in development, and ready for production.

We will go very slowly, step-by-step, like pair-programming — I’ll explain every folder, every line, every setting, and we’ll test together so you see exactly what happens.

Goal for This Lesson

By the end you will have:

  • A global CSS file that applies to every page
  • An optional app-specific CSS file for polls pages
  • Everything linked correctly from base.html
  • Styles visible on all pages
  • No 404 errors
  • Ready for production (collectstatic)

Step 1 – Create the Folders (Very Important Structure)

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

Bash

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

Bash

Why the extra polls/ inside static/? → Namespacing. If you later add a blog app that also has style.css, Django knows which one belongs to which app.

Final structure:

text

Step 2 – Tell Django Where to Find These Files

Open mysite/settings.py

Make sure (or add) these lines:

Python

Important — without STATICFILES_DIRS, Django will not look in your static/ folder.

Step 3 – Create the Global CSS File

File: static/css/global.css

CSS

This is a clean, modern base — you can expand it later.

Step 4 – (Optional) App-Specific CSS

File: polls/static/polls/css/polls.css

CSS

Step 5 – Link the Files in base.html

Open templates/base.html

Add at the very top (after {% extends %} if you have it):

HTML

Then in <head>:

HTML

Step 6 – Use the Styles in Your Templates

In polls/templates/polls/index.html (example):

HTML

→ Now all pages have global styles + polls pages get extra poll-specific styles

Step 7 – Test It

  1. Save all files
  2. Restart server if needed: python manage.py runserver
  3. Open /polls/ or any page
  4. Check:
    • Background color, font, button styles from global.css
    • Poll-card border & hover effect from polls.css
    • No 404 in browser console (F12 → Network tab)

If 404 still happens:

  • Forgot {% load static %} → add at top
  • Wrong path → check exactly static/css/global.css (case-sensitive!)
  • Server not restarted after adding files

Step 8 – Production Step (When You Deploy)

Run once before deploy:

Bash

→ All files copied to staticfiles/

WhiteNoise (if configured) serves them automatically.

Your Quick Task Right Now (Do This – It Will Stick)

  1. Create static/css/global.css with the code above
  2. Add {% load static %} + <link href=”{% static ‘css/global.css’ %}”> to base.html
  3. Restart server → check if header, body, buttons look different
  4. Create polls/static/polls/css/polls.css → add .poll-card { border: 3px solid green; }
  5. In index.html → add class poll-card to a div → see the border
  6. Check browser console → confirm no 404

Tell me what feels next:

  • “Done! Now show me how to add Tailwind CSS (CDN or local)”
  • “How to add a favicon and apple-touch-icon properly?”
  • “What if I want to use SCSS / PostCSS / Tailwind build step?”
  • “Got 404 or style not applying – here’s screenshot/error”
  • Or finally ready for Django Forms + Voting + POST + F() + results page

You now have global + app-specific CSS working beautifully — your site is starting to look like a real, modern web app.

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

You may also like...

Leave a Reply

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