【发布时间】:2017-08-01 16:41:39
【问题描述】:
我写了一个视图和模板来上传两个具有两个不同表单字段的文件,但不幸的是它只上传了一个来自第一个输入文件的文件。
这是一个模板文件,应该将两个文件上传到媒体文件夹: 模板.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')
如何上传具有多个表单输入字段的多个文件
【问题讨论】:
标签: python django-models django-forms django-templates