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

ImageField और FileField

ImageField और FileField एक model पर labeled mailboxes जैसे हैं — FileField किसी भी तरह की file accept करता है, जबकि ImageField सिर्फ pictures accept करता है और जांचता है कि वे असली images हैं।
Syntax
markup
class ModelName(models.Model):
    document = models.FileField(upload_to='folder/')
    photo = models.ImageField(upload_to='folder/')  # needs Pillow

एक FileField Define करना

FileField कोई भी uploaded file accept करता है और इसका path MEDIA_ROOT के सापेक्ष store करता है; upload_to uploads को एक subdirectory में organize करता है।

उदाहरण: Defining a FileField

FileField accepts any uploaded file and stores its path relative to MEDIA_ROOT; upload_to organizes uploads into a subdirectory.

markup
from django.db import models

class Document(models.Model):
    title = models.CharField(max_length=100)
    file = models.FileField(upload_to='documents/')
{# 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. #}

एक ImageField Define करना

ImageField FileField जैसा behave करता है लेकिन additionally यह validate करता है कि uploaded content एक genuine image है, और इसके लिए Pillow package चाहिए।

Note: ImageField उपयोग करने से पहले pip के साथ Pillow install करें, नहीं तो Django एक ImproperlyConfigured-जैसा error raise करता है।

उदाहरण: Defining an ImageField

ImageField behaves like FileField but additionally validates that the uploaded content is a genuine image, and requires the Pillow package to do so.

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

Uploaded File Access करना

दोनों fields saved instance पर .url और .path attributes expose करते हैं ताकि एक template या view uploaded file को reference कर सके।

उदाहरण: Accessing the Uploaded File

Both fields expose .url and .path attributes on the saved instance so a template or view can reference the uploaded file.

markup
profile = Profile.objects.get(id=1)
print(profile.avatar.url)
print(profile.avatar.path)
{# 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. ImageField उपयोग करना बिना Pillow installed के — Django एक error raise करता है क्योंकि ImageField images validate करने के लिए इसी पर depend करता है।
  2. upload_to argument भूल जाना, जो uploaded files को बिना organized storage subdirectory के छोड़ देता है।
  3. Field की database value को file content मानना — यह सिर्फ एक path string store करता है; असली file disk पर MEDIA_ROOT के under रहती है।
चैप्टर सारांश
  • FileField किसी भी file type को accept करता है; ImageField एक specialized FileField है जो validate करता है कि upload एक असली image है।
  • ImageField के लिए Pillow library installed होनी चाहिए।
  • upload_to वह subdirectory (MEDIA_ROOT के सापेक्ष) set करता है जहां uploaded files store होती हैं।
  • दोनों fields database में एक file path string store करती हैं, file की binary content नहीं।

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.