Static Files का Overview
In this page:
एक Static File में क्या count होता है
Static files वे assets हैं जो per request नहीं बदलते: CSS stylesheets, JavaScript files, images, और fonts। Django का staticfiles app इन्हें locate और serve करता है।
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.
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.
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 करे।
उदाहरण: 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.
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. #}
- यह मान लेना कि
STATICFILES_DIRSsimple projects के लिए भी required है — Django बिना इसके एक app का अपनाstatic/folder serve कर सकता है। - Deploy करने से पहले
collectstaticrun करना भूल जाना, इसलिए production CSS और JS के लिए 404s serve करता है। /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 में इकट्ठा करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: