0% found this document useful (0 votes)
20 views3 pages

Django File Handling Notes Manjunath

The document provides notes on file handling in Django, including how to upload files using views, create a file upload form, and manage file storage locations. It also covers creating a model with a FileField, displaying uploaded files in templates, deleting files, uploading multiple files, and validating file types. Key code snippets and configurations for settings and URLs are included for reference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Django File Handling Notes Manjunath

The document provides notes on file handling in Django, including how to upload files using views, create a file upload form, and manage file storage locations. It also covers creating a model with a FileField, displaying uploaded files in templates, deleting files, uploading multiple files, and validating file types. Key code snippets and configurations for settings and URLs are included for reference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DJANGO FILE HANDLING NOTES

1. Uploading Files (Views)

from django.core.files.storage import FileSystemStorage

def upload_file(request):

if request.method == 'POST' and request.FILES['myfile']:

myfile = request.FILES['myfile']

fs = FileSystemStorage()

filename = fs.save(myfile.name, myfile)

file_url = fs.url(filename)

return render(request, 'upload.html', {'file_url': file_url})

return render(request, 'upload.html')

2. File Upload Form (Template)

<form method="POST" enctype="multipart/form-data">

{% csrf_token %}

<input type="file" name="myfile">

<button type="submit">Upload</button>

</form>

3. File Storage Location

- By default, files are stored in MEDIA_ROOT.

In settings.py:

MEDIA_URL = '/media/'

MEDIA_ROOT = BASE_DIR / 'media'


In project urls.py:

from django.conf import settings

from django.conf.urls.static import static

urlpatterns = [

# your urls here

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

4. Model with FileField

from django.db import models

class Document(models.Model):

upload = models.FileField(upload_to='uploads/')

5. Handling Files from Model (View)

def show_files(request):

documents = Document.objects.all()

return render(request, 'files.html', {'documents': documents})

6. Displaying Files in Template

{% for doc in documents %}

<a href="{{ doc.upload.url }}">{{ doc.upload.name }}</a><br>

{% endfor %}

7. Deleting Files (Manual)

import os
def delete_file(request, file_id):

doc = Document.objects.get(id=file_id)

if os.path.isfile(doc.upload.path):

os.remove(doc.upload.path)

doc.delete()

return redirect('show_files')

8. Upload Multiple Files

Use form input with multiple attribute:

<input type="file" name="files" multiple>

Loop over request.FILES.getlist('files') in views.

9. Validating File Types

from django.core.exceptions import ValidationError

def validate_file_extension(value):

if not value.name.endswith('.pdf'):

raise ValidationError('Only PDF files are allowed.')

Use in model:

upload = models.FileField(upload_to='uploads/', validators=[validate_file_extension])

You might also like