gpt4 book ai didi

python - 在django中单页上传两个文件

转载 作者:行者123 更新时间:2023-12-01 09:30:54 25 4
gpt4 key购买 nike

我是 django 新手。我想在单页中上传两个文件。我已经创建了用于上传一个文件的表单。但是使用相同的代码,我尝试通过进行一些更改来上传两个文件。但我无法得到它。

请帮助我在单个页面的一个提交按钮中提交两个文件

views.py

from __future__ import unicode_literals

from django.shortcuts import render

from django.conf import settings

from django.core.files.storage import FileSystemStorage

from django.conf.urls import url

#import csv

def simple_upload(request):
if request.method == 'POST' and request.FILES['myfile']:
#request.FILES['myfile'] and request.FILES["myfile1"]:
myfile = request.FILES['myfile']
#myfile1=request.FILES["myfile1"]
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
#filename1=fs.save(myfile1.name, myfile1)
uploaded_file_url = fs.url(filename)
#uploaded_file_url1 = fs.url(filename1)
#data = [row for row in csv.reader(myfile.read().splitlines())]

return render(request, 'myapp/simple_upload.html', {
'uploaded_file_url': uploaded_file_url,
})

#upload_file = request.FILES['upload_file']
#data = [row for row in csv.reader(upload_file.read().splitlines())]

return render(request, 'myapp/simple_upload.html')
def home(request):
return render(request,'myapp/home.html')

html

  <!doctype>
<html>

{% block content %}
<body>
<div class="col-md-12">
<form action="{% url "home" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="row">
<label for="fileupload" class="btn btn-primary col-md-2 col-sm-4 uploadBtn" >Upload GSTR 2A</label><br>
<input type="file" class="fileupload" id="fileupload" name="myfile" required="True">
</div>
<div class="col-md-1 col-sm-2" style="text-align:center">
<i class="fa fa-check-circle checkIcon"></i>
</div>
<div class="col-md-1 col-sm-2">
<p class="cancel">X</p>
</div>
</div>
<div class="col-md-12">
<form action="{% url "home" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="row">
<label for="fileupload" class="btn btn-primary col-md-2 uploadBtn" >Upload Purchase Account</label><br>
<input type="file" class="fileupload" id="fileupload" name="myfile" required="True" >
<div class="col-md-1">
<a"<p class="cancel">X</p><a>
</div>
</div>
<div class="buttonGroup">
<button type="submit" class="btn btn-primary recBtn">Reconcile</button>&nbsp;<span class="backBtn"> Back</span>
</div>
</form>
</div>
{% endblock %}
</body>
</html>

最佳答案

您只需要在表单中输入两个类型的文件输入,然后将其放入 View 中,就像您创建的第一个文件一样,如下所示:

<form method="post" enctype="multipart/form-data" required="True">
{% csrf_token %}
<input type="file" name="myfile">
<input type="file" name="myfile2">
<button type="submit">Upload</button>
</form>

在您的 View 中,您会像第一个 View 一样获取并保存它:

myfile = request.FILES['myfile']
myfile2 = request.FILES['myfile2']

如果您需要在表单之外添加输入文件,则需要向输入文件添加“form”属性。像这样:

    <input type="file" name="myfile2" form="FORM_ID">

只需将 FORM_ID 替换为您的表单 ID。

关于python - 在django中单页上传两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49973818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com