0
我已經寫下了一個視圖和模板,用兩個不同的表單域上傳兩個文件,但不幸的是,它只上傳一個來自第一個輸入文件的文件。用Django上傳多個表單域的文件?
這是應該上傳兩個文件到媒體文件夾模板文件: tamplate.html
{% extends 'protocol/base.html' %}
{% load staticfiles %}
{% block content %}
<style type="text/css">
.button {
background-color: #ff9800; /* Green */
border: none;
color: black;
padding: 10px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin-left: 250px;
margin-top: 10px;
margin-bottom: 30px;
}
.input {
margin-left: 250px;
}
.text {
margin-left: 250px;
}
.heading{
margin-left: 250px;
}
</style>
<form method="post" enctype="multipart/form-data">
<div class="heading">
<h1> Machine Learning Modeling.. </h1>
<h2> Upload CSV files contains both, positive and Negative peptides.. </h2>
</div>
{% csrf_token %}
<input class="input" type="file" name="myfile">
<input class="input" type="file" name="myfile_1">
<button class="button" type="submit">Upload</button>
</form>
{% if uploaded_file_url %}
<p class="text" >File uploaded successfully. </p>
{% endif %}
{% endblock %}
Views.py
from django.shortcuts import render
from django.core.files.storage import FileSystemStorage
from django.shortcuts import render, redirect
from django.conf import settings
import os
import glob
from django.core.files.storage import FileSystemStorage
#from django.core.servers.basehttp import FileWrapper
from wsgiref.util import FileWrapper
import urllib, mimetypes
from django.http import HttpResponse
from django.utils.encoding import smart_str
# Create your views here.
def predict(request):
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
if request.method == 'POST' and request.FILES['myfile_1']:
myfile = request.FILES['myfile_1']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
return render(request, 'prediction/thanks.html', {})
return render(request, 'prediction/prediction.html')
如何上傳用的多輸入多領域的文件表格