【发布时间】:2020-06-01 12:50:58
【问题描述】:
我想在表单中上传多个文件或文件夹,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 管理面板中显示
【问题讨论】:
-
尝试在你的表单标签中添加这个
enctype="multipart/form-data"。 -
已添加但无法使用..
-
您现在可以发布您的表格吗..!
标签: django django-models django-forms django-views django-templates