ImageField और FileField
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.
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 चाहिए।
उदाहरण: 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.
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.
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. #}
- ImageField उपयोग करना बिना Pillow installed के — Django एक error raise करता है क्योंकि ImageField images validate करने के लिए इसी पर depend करता है।
- upload_to argument भूल जाना, जो uploaded files को बिना organized storage subdirectory के छोड़ देता है।
- 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 नहीं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: