Development में Static Files Serve करना
In this page:
# 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 चाहिए नहीं।
{% 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.
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.
# 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 चाहिए।
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.
# 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. #}
- Production में Django की development static file serving पर भरोसा करना — यह scale पर slow और insecure है, सिर्फ local development के लिए बनी है।
- यह भूल जाना कि
runserverको बिना extra URL configuration के static files auto-serve करने के लिएDEBUG=Trueचाहिए। STATICFILES_DIRSमें एक नईstatic/directory जोड़ने के बाद development server restart न करना।
- Django का
runserverstatic files को अपने आप serve करता है जबDEBUGTrueहो। - यह convenience production के लिए नहीं है — असली deployments इसके बजाय एक web server या CDN उपयोग करते हैं।
STATICFILES_DIRSआपको हर app की खुद कीstatic/directory के अलावा extra folders जोड़ने देता है।- Development के दौरान static files refresh पर immediately update होती हैं, कोई
collectstaticstep चाहिए नहीं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: