MEDIA_URL का Configuration
In this page:
# 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.
# 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 करना है।
उदाहरण: 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.
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.
<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. #}
- Development के दौरान urls.py में media URL pattern जोड़ना भूल जाना, इसलिए uploaded files 404 देती हैं भले ही वे disk पर मौजूद हों।
- Production में django.conf.urls.static.static() उपयोग करना — यह एक development-only convenience है, असली deployments के लिए नहीं।
- एक 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 बनाता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: