Media Files का Overview
# settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
Media बनाम Static Files
Static files आपके code के साथ ship होती हैं (CSS, JS, images जो आपने design की)। Media files users द्वारा runtime पर बाद में upload होती हैं (avatars, attachments) और इन्हें अपनी storage location चाहिए।
उदाहरण: Media vs Static Files
Static files are shipped with your code (CSS, JS, images you designed). Media files are uploaded later by users at runtime (avatars, attachments) and need their own storage location.
# settings.py
MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'
{# 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. #}
Uploads कहां Store होती हैं
MEDIA_ROOT वह filesystem directory है जिसमें Django uploaded files save करता है; MEDIA_URL वह URL prefix है जो उन्हें वापस browser को serve करने के लिए उपयोग होता है।
उदाहरण: Where Uploads Are Stored
MEDIA_ROOT is the filesystem directory Django saves uploaded files into; MEDIA_URL is the URL prefix used to serve them back to the browser.
MEDIA_ROOT = BASE_DIR / 'media'
# An uploaded avatar might land at:
# media/avatars/profile1.jpg
{# 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. #}
Uploads Accept करने वाला एक Model
एक model को एक uploaded file का path database में store करने के लिए FileField या ImageField चाहिए, जबकि असली file MEDIA_ROOT के under रहती है।
उदाहरण: A Model That Accepts Uploads
A model needs a FileField or ImageField to store an uploaded file's path in the database while the actual file lives under MEDIA_ROOT.
from django.db import models
class Profile(models.Model):
name = models.CharField(max_length=100)
avatar = models.ImageField(upload_to='avatars/')
{# 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. #}
- Media files (user-uploaded content) को static files (developer-provided assets) के साथ confuse करना — वे अलग settings उपयोग करती हैं।
- यह भूल जाना कि MEDIA_ROOT exist करना चाहिए और writable होना चाहिए, नहीं तो uploads fail होंगी।
- User uploads को project की static/ directory से सीधे serve करना, बजाय एक dedicated media directory के।
- Media files user-uploaded content हैं, जैसे profile pictures या attached documents।
- MEDIA_URL और MEDIA_ROOT STATIC_URL और STATIC_ROOT के media equivalents हैं।
- Django कभी media file content process नहीं करता — यह सिर्फ user जो भी upload करे उसे store और serve करता है।
- Media handling के लिए model पर FileField या ImageField चाहिए, साथ में form पर enctype='multipart/form-data'।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: