File Upload के Forms
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="file_field">
<button type="submit">Upload</button>
</form>
# view
uploaded = request.FILES['file_field']
Upload Form Template
File uploads काम करने के लिए form tag को method=post और enctype='multipart/form-data' चाहिए; बिना enctype वाला plain form सिर्फ filename को text के रूप में submit करेगा, असली file नहीं।
उदाहरण: The Upload Form Template
The form tag needs method=post and enctype='multipart/form-data' for file uploads to work; a plain form without enctype will submit the filename as text only, not the file itself.
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<label for="id_avatar">Avatar</label>
<input type="file" id="id_avatar" name="avatar">
<button type="submit">Upload</button>
</form>
{# 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. #}
View में Upload Handle करना
Uploaded file data request.FILES में आता है, request.POST में regular form fields से अलग। दोनों को request से bound एक ModelForm में pass किया जाना चाहिए।
उदाहरण: Handling the Upload in a View
Uploaded file data arrives in request.FILES, separate from regular form fields in request.POST. Both must be passed into a ModelForm bound to the request.
from django.shortcuts import render, redirect
from .forms import ProfileForm
def upload_avatar(request):
if request.method == 'POST':
form = ProfileForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('profile')
else:
form = ProfileForm()
return render(request, 'upload.html', {'form': form})
{# 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 Files Validate करना
FileField या ImageField उपयोग करने वाला एक form field के through अपने आप upload validate करता है — form.is_valid() call करना form.save() चलने से पहले missing या invalid files reject करता है।
उदाहरण: Validating Uploaded Files
A form using a FileField or ImageField automatically validates the upload through the field itself — calling form.is_valid() rejects missing or invalid files before form.save() ever runs.
from django import forms
from .models import Profile
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['name', 'avatar']
form = ProfileForm(data, files)
if form.is_valid():
form.save()
else:
print(form.errors)
{# 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. #}
- <form> tag पर enctype='multipart/form-data' भूल जाना, जो चुपचाप file को upload होने से पूरी तरह रोकता है।
- Uploaded data request.FILES के बजाय request.POST से पढ़ना।
- Save करने से पहले file size या type validate न करना, जो oversized या unexpected files को store होने देता है।
- File upload forms में enctype='multipart/form-data' set होना चाहिए नहीं तो file data कभी server तक नहीं पहुंचती।
- Uploaded files request.FILES के through access होती हैं, request.POST से नहीं।
- हर file input को अब भी एक label और एक name attribute चाहिए बिल्कुल किसी और form field की तरह।
- FileField/ImageField उपयोग करने वाले form को दोनों request.POST और request.FILES के साथ instantiate किया जाना चाहिए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: