Django FileField -如何将文件保存到不同的目录

vsmadaxz  于 2023-07-01  发布在  Go
关注(0)|答案(1)|浏览(146)

我有两个类似的形式上传文件,但我想保存文件在不同的目录取决于形式。
让我解释一下,例如:
如果用户从Form_1上传文件->文件应保存在media/folder_file_1/file.csv
如果用户从Form_2上传文件->文件应保存在media/folder_file_2/file.csv
关于models.py, forms.py, views.py, urls.py,我只是使用了Django文档中的示例。

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)

url.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'),
]
relj7zay

relj7zay1#

好的,我的做法是:

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)

通过这种方式,它将文件存储在您在www.example.com中分配的文件夹中settings.py作为MEDIA_ROOT。然后将其放入与您的用户名同名的文件夹中。然后是文件名。
如果用户名作为文件夹名是一个很好的选择是肯定有争议的,但你得到的想法。
在这里找到另一个很好的例子:FileField.upload_to

相关问题