1

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'),
]
macder
  • 145
  • 9
  • But what is the difference of the two forms? If there is entirely no difference it does not make sense to put them into separate folders. So what is the reason you want to separate files from form1 to files from form2? – Tarquinius Jun 30 '23 at 07:26

1 Answers1

1

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

Tarquinius
  • 1,468
  • 1
  • 3
  • 18
  • Hello @Tarquinius, do I need do something from html side? Are the python forms somehow related to the html elements (forms), idk? – macder Jun 29 '23 at 07:41
  • 1
    My solution figures the folder path by looking at the related model of the form which is holding the file. I agree, it does not differentiate between both forms. But the forms are the same anyway. Maybe it would be helpful to describe your specific scenario and not an example. – Tarquinius Jun 29 '23 at 08:28