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

Media Files का Overview

Media files वे चीज़ें हैं जो users खुद upload करते हैं, जैसे एक photo जो वे अपनी profile पर attach करते हैं, static files के विपरीत जो आप developer के रूप में पहले से provide करते हैं।
Syntax
markup
# 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.

markup
# 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 करने के लिए उपयोग होता है।

Note: MEDIA_ROOT को STATIC_ROOT के बाहर रखें ताकि collectstatic कभी user uploads को न छुए।

उदाहरण: 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.

markup
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.

markup
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. #}
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. Media files (user-uploaded content) को static files (developer-provided assets) के साथ confuse करना — वे अलग settings उपयोग करती हैं।
  2. यह भूल जाना कि MEDIA_ROOT exist करना चाहिए और writable होना चाहिए, नहीं तो uploads fail होंगी।
  3. 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'।

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.