File upload handling lets users send files to your website. This helps you save images, documents, or other files from users.
0
0
File upload handling basics in Django
Introduction
When users need to upload profile pictures.
When users submit documents like resumes or reports.
When users want to share images or videos on your site.
When you build forms that accept file attachments.
When you want to store user files on your server.
Syntax
Django
In your Django form: from django import forms class UploadForm(forms.Form): file = forms.FileField() In your view: def upload_file(request): if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES['file']) # process file else: form = UploadForm() return render(request, 'upload.html', {'form': form}) Helper function: def handle_uploaded_file(f): with open('some/path/' + f.name, 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk)
Use request.FILES to access uploaded files.
Always save files in chunks to avoid memory issues.
Examples
This creates a simple form with a file input field.
Django
class UploadForm(forms.Form): file = forms.FileField(label='Select a file')
This view handles the file upload and saves the file.
Django
def upload_file(request): if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): file = request.FILES['file'] handle_uploaded_file(file) return HttpResponse('File uploaded!') else: form = UploadForm() return render(request, 'upload.html', {'form': form})
This function saves the uploaded file in chunks to the 'uploads' folder.
Django
def handle_uploaded_file(f): with open('uploads/' + f.name, 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk)
Sample Program
This Django code defines a form to upload a file, a helper to save it, and a view to handle the upload process. When a user submits a file, it saves the file to the 'uploads' folder and shows a success message.
Django
from django import forms from django.http import HttpResponse from django.shortcuts import render class UploadForm(forms.Form): file = forms.FileField(label='Choose a file') def handle_uploaded_file(f): with open('uploads/' + f.name, 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) def upload_file(request): if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES['file']) return HttpResponse('File uploaded successfully!') else: form = UploadForm() return render(request, 'upload.html', {'form': form})
OutputSuccess
Important Notes
Make sure the 'uploads' folder exists and has write permission.
Use enctype="multipart/form-data" in your HTML form tag to allow file uploads.
Validate file size and type to keep your app safe.
Summary
File uploads let users send files to your Django app.
Use forms.FileField and request.FILES to handle uploads.
Save files in chunks to avoid memory problems.