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

Development में Static Files Serve करना

Development में, Django खुद आपके CSS और images के लिए delivery driver की तरह play करता है, इसलिए आपको build करते समय अपनी styles देखने के लिए एक separate web server नहीं चाहिए।
Syntax
markup
# settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']

runserver से Automatic Serving

जब DEBUG=True हो, django.contrib.staticfiles अपने आप एक URL pattern wire कर देता है ताकि runserver हर static file serve कर सके जो यह ढूंढे, कोई manual configuration चाहिए नहीं।

Note: सिर्फ server start करें और files को {% static %} से reference करें — बाकी Django handle करता है।

उदाहरण: Automatic Serving with runserver

When DEBUG=True, django.contrib.staticfiles automatically wires up a URL pattern so runserver can serve every static file it finds, no manual configuration needed.

bash
python manage.py runserver

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

Extra Static Directories जोड़ना

STATICFILES_DIRS additional folders list करता है (किसी एक app के बाहर) जिन्हें static files के लिए भी search किया जाना चाहिए, जैसे एक project-wide static/ directory।

उदाहरण: Adding Extra Static Directories

STATICFILES_DIRS lists additional folders (outside any single app) that should also be searched for static files, such as a project-wide static/ directory.

markup
# settings.py
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. #}

यह Production में क्यों काम नहीं करता

Development server की static file serving intentionally simple और unoptimized है। Production में, DEBUG False होता है और यह auto-serving बंद हो जाती है, इसलिए इसके बजाय एक असली web server या collectstatic-backed setup चाहिए।

Warning: Production में सिर्फ static files काम करते रखने के लिए कभी DEBUG=True set न करें — यह sensitive debug information भी expose करता है।

उदाहरण: Why This Doesn't Work in Production

The development server's static file serving is intentionally simple and unoptimized. In production, DEBUG is False and this auto-serving stops, so a real web server or collectstatic-backed setup is required instead.

markup
# settings.py (production)
DEBUG = False
# Static files now need collectstatic + a real web server
{# 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. Production में Django की development static file serving पर भरोसा करना — यह scale पर slow और insecure है, सिर्फ local development के लिए बनी है।
  2. यह भूल जाना कि runserver को बिना extra URL configuration के static files auto-serve करने के लिए DEBUG=True चाहिए।
  3. STATICFILES_DIRS में एक नई static/ directory जोड़ने के बाद development server restart न करना।
चैप्टर सारांश
  • Django का runserver static files को अपने आप serve करता है जब DEBUG True हो।
  • यह convenience production के लिए नहीं है — असली deployments इसके बजाय एक web server या CDN उपयोग करते हैं।
  • STATICFILES_DIRS आपको हर app की खुद की static/ directory के अलावा extra folders जोड़ने देता है।
  • Development के दौरान static files refresh पर immediately update होती हैं, कोई collectstatic step चाहिए नहीं।

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.