← Back to Django Course | Chapter 11: Static Files & Media Handling | Lesson 1 of 8

Static Files का Overview

Static files वे pictures, style sheets, और scripts हैं जो एक website बिना बदले exactly वैसे ही handle out करती है, जैसे हर बार एक नया लिखने के बजाय किसी को एक printed flyer देना।

एक Static File में क्या count होता है

Static files वे assets हैं जो per request नहीं बदलते: CSS stylesheets, JavaScript files, images, और fonts। Django का staticfiles app इन्हें locate और serve करता है।

Note: दूसरी apps के साथ naming collisions से बचने के लिए static files को एक app के static/<app_name>/ folder के अंदर रखें।

उदाहरण: What Counts as a Static File

CSS, JS, images, and fonts all belong under an app's static/ folder — these commands create that folder structure and two static files inside it.

bash
mkdir -p myapp/static/myapp
touch myapp/static/myapp/style.css myapp/static/myapp/script.js

⚠️ Run this in your own terminal or Node.js environment.

Staticfiles App Enable करना

django.contrib.staticfiles को INSTALLED_APPS में listed होना चाहिए — यह एक नए project में default रूप से enabled ship होता है और static files discover और serve करने को handle करता है।

उदाहरण: Enabling the Staticfiles App

django.contrib.staticfiles must be listed in INSTALLED_APPS — it ships enabled by default in a new project and handles discovering and serving static files.

markup
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.staticfiles',
    'myapp',
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Django Static Files कैसे ढूंढता है

Django हर installed app की static/ subdirectory अपने आप search करता है, साथ ही STATICFILES_DIRS में listed कोई भी extra directories, जब एक {% static %} tag resolve करे या development में files serve करे।

Warning: एक same relative path पर दो apps silently एक-दूसरे को shadow कर सकती हैं — app name से namespace करें।

उदाहरण: How Django Finds Static Files

Django searches each installed app's static/ subdirectory automatically, plus any extra directories listed in STATICFILES_DIRS, when resolving a {% static %} tag or serving files in development.

markup
STATICFILES_DIRS = [
    BASE_DIR / 'static',
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि STATICFILES_DIRS simple projects के लिए भी required है — Django बिना इसके एक app का अपना static/ folder serve कर सकता है।
  2. Deploy करने से पहले collectstatic run करना भूल जाना, इसलिए production CSS और JS के लिए 404s serve करता है।
  3. /static/style.css जैसे static file paths hardcode करना बजाय {% static %} tag उपयोग करने के, जो STATIC_URL बदलने पर टूट जाता है।
चैप्टर सारांश
  • Static files CSS, JavaScript, images, और fonts हैं जो Django browser को बिना बदले serve करता है।
  • हर app अपनी खुद की static/ subdirectory रख सकती है, और Django का staticfiles app इन सबको साथ collect करता है।
  • STATIC_URL वह URL prefix define करता है जो browsers इन files request करने के लिए उपयोग करते हैं।
  • Production में, collectstatic हर app की static files को web server serve करने के लिए एक directory में इकट्ठा करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.