我想以表格形式上传多个文件或一个文件夹,html表格成功地上传了多个文件,但是当涉及到django处理时,它向我展示了夜空。 我已经创建了这样的html表单
<input type="file" name="file" multiple />
我已经创建了这样的模型
class Report(models.Model):
name = models.CharField(max_length=60)
report = models.CharField(max_length=10)
task = models.CharField(max_length=60)
date = models.DateField(null=True)
start_time = models.TimeField()
end_time = models.TimeField()
no_of_hours = models.CharField(max_length=20)
team_lead = models.CharField(max_length=30)
today_progress = models.CharField(max_length = 1000)
file_input = models.FileField(upload_to='documents/')
concern = models.CharField(max_length=1000)
next_plan = models.CharField(max_length=1000)
next_plan_file = models.FileField(upload_to='next/')
像这样的views.py
def save(request):
report_object = Report()
report_object.name = request.POST["name"]
report_object.report = request.POST["report"]
report_object.task = request.POST["task"]
report_object.date = request.POST["date"]
report_object.start_time = request.POST["start_time"]
report_object.end_time = request.POST["end_time"]
report_object.no_of_hours = request.POST["no_of_hours"]
report_object.team_lead = request.POST["team_lead"]
report_object.today_progress = request.POST["today_progress"]
report_object.file_input = request.FILES.["file_input"]
report_object.concern = request.POST["concern"]
report_object.next_plan = request.POST["next_plan"]
report_object.next_plan_file = request.FILES.["upload_next"]
report_object.save()
return redirect('/')
我将文件传递到我创建的模型中,但是只有最后一个文件被插入并显示在管理面板中
现在我只得到最后一个文件(如果我选择3个文件,则得到第3个文件)。如何获取所有文件或文件夹? 甚至想在Django管理面板中显示
答案 0 :(得分:0)
尝试一下,看看能不能成功!
替换
file=request.FILES.get('file')
使用
files = request.FILES.getlist('file')
您必须遍历视图中的每个元素
if form.is_valid():
name = form.cleaned_data['name']
for f in files:
File.objects.create(name=name, file=f)
return HttpResponse('OK')
名称是您的模型字段,并将所有上传的文件保存在名称字段