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

MEDIA_URL का Configuration

MEDIA_URL browser को बताता है कि uploaded files के लिए कहां देखना है, और development में Django को उन files को असल में hand out करने के लिए एक extra line चाहिए।
Syntax
markup
# settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

# urls.py (development only)
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

MEDIA_URL और MEDIA_ROOT Set करना

MEDIA_URL uploads के लिए public URL prefix है; MEDIA_ROOT वह filesystem directory है जिसमें वे असल में save होती हैं। Media uploads काम करने के लिए दोनों चाहिए।

उदाहरण: Setting MEDIA_URL and MEDIA_ROOT

MEDIA_URL is the public URL prefix for uploads; MEDIA_ROOT is the filesystem directory they're actually saved to. Both are required for media uploads to work.

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. #}

Development में Media Files Serve करना

Static files के विपरीत, media files को development में एक explicit URL pattern जोड़ना पड़ता है ताकि Django को पता चले कि उन्हें MEDIA_ROOT से serve करना है।

Note: यह pattern सिर्फ तभी जोड़ा जाना चाहिए जब DEBUG True हो — यह production उपयोग के लिए नहीं है।

उदाहरण: Serving Media Files in Development

Unlike static files, media files need an explicit URL pattern added in development so Django knows to serve them from MEDIA_ROOT.

markup
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... your other patterns
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
{# 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. #}

एक Template में Uploaded File Reference करना

एक FileField/ImageField का .url attribute पहले से MEDIA_URL शामिल करता है, इसलिए templates manual string building के बिना सीधे इसे link कर सकते हैं।

उदाहरण: Referencing an Uploaded File in a Template

A FileField/ImageField's .url attribute already includes MEDIA_URL, so templates can link directly to it without manual string building.

markup
<img src="{{ profile.avatar.url }}" alt="Avatar">
{# 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. Development के दौरान urls.py में media URL pattern जोड़ना भूल जाना, इसलिए uploaded files 404 देती हैं भले ही वे disk पर मौजूद हों।
  2. Production में django.conf.urls.static.static() उपयोग करना — यह एक development-only convenience है, असली deployments के लिए नहीं।
  3. एक uploaded file का template link बनाते समय MEDIA_URL को MEDIA_ROOT के साथ mix up करना।
चैप्टर सारांश
  • MEDIA_URL वह URL prefix है जो user-uploaded files serve करने के लिए उपयोग होता है।
  • Development में, urls.py को explicitly MEDIA_ROOT से files serve करने के लिए एक pattern जोड़ना चाहिए।
  • Production में, एक असली web server (Django खुद नहीं) को media files serve करनी चाहिए।
  • एक model का FileField/ImageField इसके .url attribute के through पहले से सही URL बनाता है।

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.