在django中单页上传两个文件

时间:2018-04-23 05:16:01

标签: python django

我是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>

1 个答案:

答案 0 :(得分:1)

您只需要在表单中包含两个类型文件输入,并在视图中将其与您创建的第一个完全相同,如下所示:

<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>

在你的观点中,你得到它并像第一个一样保存它:

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

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

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

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