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

File Upload के Forms

File upload form किसी को envelope देने जैसा है बजाय सिर्फ बात करने के — browser को file को specially package करना पड़ता है ताकि server इसे unwrap कर सके।
Syntax
markup
<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.

markup
<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.

markup
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 करता है।

Note: Stricter validation के लिए form पर एक clean_<field> method के अंदर custom size/type checks जोड़ें।

उदाहरण: 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.

markup
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. #}
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. <form> tag पर enctype='multipart/form-data' भूल जाना, जो चुपचाप file को upload होने से पूरी तरह रोकता है।
  2. Uploaded data request.FILES के बजाय request.POST से पढ़ना।
  3. 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 किया जाना चाहिए।

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.