Django FileField - how to save files to different directories

118 Views Asked by At

I have two similar forms for upload files, but I want to save files in different directories depending on the form.

Let me explain, for example:

if User uploaded file from Form_1 -> file should be save in media/folder_file_1/file.csv

if User uploaded file from Form_2 -> file should be save in media/folder_file_2/file.csv

Regarding models.py, forms.py, views.py, urls.py I just used examples from Django docs.

index.html:

<!DOCTYPE html>
{% load static %}

<body>
<div class="page_secondblock secondblock">
    <div class="secondblock__container _container">
        <h1 class="secondblock__title">
            The file you want to update:   
        </h1>
        <h2 class="secondblock__title">
            The file from which you want to get information:
        </h2>
    </div>
</div>
<div class="page_thirdblock thirdblock">
    <div class="thirdblock__container _container">
        <form method="POST" enctype="multipart/form-data" class="upload1" id="upload_container"> 
            {% csrf_token %}
            {{form.as_p}}
            <input type="submit" value="Submit">
        </form>
        <form method="POST" enctype="multipart/form-data" class="upload2" id="upload_container">
            {% csrf_token %}
            {{form.as_p}}
            <input type="submit" value="Submit">
        </form>
    </div>
</div>
</body>

models.py:

from django.db import models

class UploadFile(models.Model):
    file = models.FileField(upload_to='working/')

forms.py:

from django import forms  
from .models import UploadFile

class UploadFileForm(forms.ModelForm):
    class Meta:
        model = UploadFile
        fields = ['file']

views.py:

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect 
from .forms import UploadFileForm
  
def index(request):
    return render(request, "index.html")
 
def tools(request):
    return render(request, "index.html")
 
def login(request):
    return render(request, "index.html")

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST,request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('home')    
    else:
        form = UploadFileForm()
        context = {
            'form':form,
        }
    return render(request, 'index.html', context)

urls.py:

from django.contrib import admin
from django.urls import path, re_path
from actualization import views

urlpatterns = [
    re_path(r'^tools', views.tools, name='tools'),
    re_path(r'^login', views.login, name='login'),
    path('get_result/', views.get_result),
    path('home/', views.upload_file, name='upload_file'),
]
1

There are 1 best solutions below

2
Tarquinius On

Ok, so the way I do it is the following:

from django.db import models

def _upload_location(instance, filename):
    return f'{instance.owner.username}/{filename}'


class UploadFile(models.Model):
    file = models.FileField(upload_to=_upload_location)
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

This way it stores the file inside of the folder you assigned in your settings.py as MEDIA_ROOT. Then puts it in a folder with the same name as your username. Afterwards follows the filename.

If the username as a foldername is a good choice is for sure debatable but you get the idea.

Find another good example here: FileField.upload_to