Django Simple File Upload
In this article, you will learn the simple process of uploading a file on the server using Django and storing it in a given directory. File uploads are becoming an essential feature in web applications like healthcare portals, insurance sites, and messaging applications for social media. Django comes with in-built packages and methods that make this task easier.
These are the step-by-step instructions for uploading a file using Django
First, we have created a new project 'uploadfile' and a new app 'uploadapp' to develop a file upload process. So, open your terminal window and run the following commands-
(env) c:\python37\Scripts\projects>django-admin startproject uploadfile
(env) c:\python37\Scripts\projects>cd uploadfile
(env) c:\python37\Scripts\projects\generatecsv>django-admin startapp uploadapp
views.py
Django comes with a FileSystemStorage class. It provides storage on the local system. In this, we can provide arguments such as the location path of local storage, base_url to access the uploaded file, file_permissions_mode to set file permissions, and directory_permissions_mode to set the access permissions of the directory.
FileSystemStorage(location, base_url, file_permissions_mode, directory_permissions_mode)
So, import the file system storage package at the top of 'views.py' like this -
from django.core.files.storage import FileSystemStorage
Here is the complete code of 'views.py' -
from django.shortcuts import render
from django.conf import settings
from django.core.files.storage import FileSystemStorage
def fileupload(request):
if request.method == 'POST' and request.FILES['uploadedfile']:
getfile = request.FILES['uploadedfile']
fss = FileSystemStorage()
filename = fss.save(getfile.name, getfile)
uploaded_file_url = fss.url(filename)
return render(request, 'fileupload.html', {
'uploaded_file_url': uploaded_file_url
})
return render(request, 'fileupload.html')
In the above file, we first checked the uploaded file and the upload method, i.e., POST, and then got the uploaded file name using request.FILES['uploadedfile']. The 'uploadedfile' is the input file name attribute value provided in the HTML file. Next, we saved the file using the save() method and got the file access url using the url() method of FileSystemStorage.
Create HTML Template file
Here, we have created an HTML file containing a form to upload a file -
<html>
<head>
<title>Django File Upload</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="uploadedfile">
<button type="submit">Upload File</button>
</form>
{% if uploaded_file_url %}
<p>File uploaded at:
<a href="/{{ uploaded_file_url }}">{{ uploaded_file_url }}</a>
</p>
{% endif %}
</body>
</html>
Media Settings
We can set the uploaded file location in two ways, either we can provide the location path in FileSystemStorage() as an argument or we can add the media storage settings in 'settings.py'. In this article, we have added the MEDIA settings in 'settings.py'.
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
So, when you upload the file, it will be stored in 'media' directory.
urls.py
Here, we have added a path to route the page to the particular link.
from django.contrib import admin
from django.urls import path
from uploadapp import views
urlpatterns = [
path('uploadfile', views.fileupload),
]
Run the Application
At last, migrate and run the project -
(env) c:\python37\Scripts\projects\uploadfile>python manage.py migrate
(env) c:\python37\Scripts\projects\uploadfile>python manage.py runserver
When you open 'http://127.0.0.1:8000/uploadfile' in the browser, you will see the file upload form. On uploading the file, it will be stored in the 'media' folder and also provide the link to access the uploaded file.
Related Articles
Django ajax GET and POST requestDjango Pagination with Ajax and jQuery
Django upload image to database
How to read and write a file using Django
Django pass variable from view to HTML template
Django Export Model Data to CSV
How to generate and download CSV file in Django
How to get data from MySQL in Django View with Models
Django Send Mail on Contact form submission
How to insert data in MySQL database from an HTML form using Django
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
PHP Connection and File Handling on FTP Server
File Upload Validation in PHP
Simple File Upload Script in PHP
Import a CSV file into MySQL using PHP
Insert image in database using PHP