我想上传包含少量文件的文件夹,并将这些文件保存到数据库中。我成功上传了一个文件,但在尝试上传文件夹时遇到了问题。这些文件不会存储在数据库中。有人可以告诉我哪里出错了。
class Document(models.Model):
report_id = models.CharField(max_length=50, blank=False)
description = models.CharField(max_length=255, blank=False)
document = models.FileField(upload_to='documents/')
uploaded_at = models.DateTimeField(auto_now_add=True)
is_diagnoised = models.BooleanField(default=False)
uploaded_by_doc = models.CharField(max_length=20, default="")
downloaded_by_rad = models.CharField(max_length=20, default="")
class DocumentForm(forms.ModelForm):
class Meta:
model = Document
fields = ('report_id', 'description', )
def index(request):
if request.user.is_authenticated:
uid = request.user.id
usern = request.user.username
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
for afile in request.FILES.getlist('document'):
new_file = Document(document = afile)
post = form.save(commit=False)
post.uploaded_by_doc = usern
post.document = form.cleaned_data.get('new_file')
post.save()
return HttpResponseRedirect('index')
else:
form = DocumentForm()
return render(request, 'upload.html', context)
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="file" name="document" multiple = "true" webkitdirectory="true" directory = "true"/>
<button type="submit">Upload</button>