Upload file to a specific folder in Django

In this tutorial, we will learn how to upload a file to a specific folder using Django.

Steps to Follow:

  1. Create a project and start an app.
  2. Create a forms.py file in the app.
  3. Create a static folder in the app.
  4. Create a function.py file.

Create a project and start an app:

First, we need to create a Django project for this we just have to run the command django-admin startproject django_project in our terminal but make sure about your current working directory. After that, we have to make an app inside our project to do so run the command python manage.py startapp blog in your current working directory which is django_project.

Now, we need to register our app. So open settings.py and in INSTALLED_APPS just write  ‘blog.apps.BlogConfig’,  this will register our app in project.

Create a forms.py file:

Open views.py file and just create a function named it as formsubmission. And now we need to pass our form so before that just create a form in our app and import the forms. In forms.py just type the below code,

from cProfile import label
from django  import forms
class upload(forms.Form):
    name = forms.CharField(label = "Enter name",max_length=250)
    email = forms.EmailField()
    file = forms.FileField()

Now we need to import this in our views.py file to do so write the below code in views.py file,

from django.http import HttpResponse
from django.shortcuts import render
from blog.forms import upload
from blog.forms import upload
from blog.functions.functions import handle_uploaded_file

# Create your views here.
def formsubmission(request):
    form = upload()
    if request.method == "POST":
        form = upload(request.POST,request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponse("FILE UPLOADED SUCCESSFULLY")
        else:
            form = upload()
    return render(request,'blog/home.html',{'form':form})

This will help us to upload the file.

Create a static folder:

Create a static folder in your app and inside that create js folder and on that create fileuploaded folder where our files are stored.

After that create a templates folder and inside that create a folder blog within that create home.html.

Now will create our form so write the below code in home.html file,

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form method="POST" enctype="multipart/form-data">
        {%csrf_token%}
        {{form.as_p}}
        <button type="submit">Save</button>
    </form>
</body>
</html>

In this, enctype is necessary for uploading multiple files.

Create a fucntions.py file to upload files to a specific directory in Django

Now create a functions.py file in function directory to do so just create a folder named function in your app and inside that create functions.py file this will handle the uploaded files.

Write the below code in fucntions.py,

def handle_uploaded_file(f):
    with open('blog/static/fileuploaded' + f.name,'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

Chunk used to read huge CSV files.

After that, we have to import our views in urls.py,

from django.contrib import admin
from django.urls import path
from blog import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.formsubmission),
]

Now that’s it just the run the server you will get this,

In this tutorial, wupload a file to a specific folder using Django

Just fill in the details and see the fileuploaded folder there will be the file you have uploaded.

Leave a Reply

Your email address will not be published.