请帮忙。我正在尝试使用CreateView在Django中保存多个文件。但是我只能将最后一个文件保存在所选列表中。然后,我将视图更改为与在此处看到的大多数解释都匹配的函数视图。.现在,我遇到了“值错误”。
下面是两个视图。 CreateView(CBV)和函数视图。 谢谢
CreateView
@method_decorator(login_required, name='dispatch')
class UpdateMatterCreateView(CreateView):
form_class = UpdateMatterForm
model = MatterUpdates
template_name = 'matter/matter_instance_create.html'
success_url = reverse_lazy('matter_list')
def form_valid(self, form):
form = UpdateMatterForm(self.request.POST, self.request.FILES)
with transaction.atomic():
form.instance.client_id = self.kwargs['client_id']
form.instance.firm_id = self.kwargs['firm_id']
form.instance.matter_id = self.kwargs['matter_id']
form.instance.user_id = self.request.user.id
files = self.request.FILES.getlist('files')
for f in files:
docs_path = Documents.objects.create(matter_update_id=self.kwargs['matter_id'], file_path=f)
docs_path.save()
return super(UpdateMatterCreateView, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(UpdateMatterCreateView, self).get_context_data(**kwargs)
context['object'] = Matter.objects.get(id=self.kwargs['matter_id'])
return context
功能视图
def update_matter_instance(request, firm_id, client_id, matter_id):
if request.method == 'POST':
form = UpdateMatterForm(request.POST, request.FILES)
if form.is_valid():
title = form.instance.title
content = form.instance.content
created_date = form.instance.created_date
created_time = form.instance.created_time
matter_update_link = MatterUpdates.objects.create(firm_id=firm_id, client_id=client_id,
matter_id=matter_id, title=title, content=content,
created_date=created_date, created_time=created_time,
user_id=request.user.id, )
matter_update_link.save()
files = request.FILES.getlist('files')
for f in files:
docs_path = Documents.objects.create(matter_update_id=matter_id, file_path=f)
docs_path.save()
return redirect('matter_list')
else:
form = UpdateMatterForm()
return render(request, 'matter/matter_instance_create.html', {'form': form})
forms.py
class UpdateMatterForm(forms.ModelForm):
class Meta:
model = MatterUpdates
exclude = ['matter', 'client', 'firm', 'sys_date', 'user']
widgets = {
'title': forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'Enter Title'}),
'content': RichTextFormField(),
'created_date': forms.DateInput(
attrs={'class': 'form-control', 'placeholder': 'mm/dd/yyyy'}),
'created_time': forms.TimeInput(
attrs={'class': 'form-control', 'id': 'timepicker2', 'type': 'text'}),
'files': forms.ClearableFileInput(attrs={'multiple': True})
}
urls.py
功能正则表达式
url(r'^legal_stallion/stallion_matter_instance_update/(?P<firm_id>[0-9A-Fa-f-]+)/(?P<client_id>[0-9A-Fa-f-]+)/'
r'(?P<matter_id>[0-9A-Fa-f-]+)/$', views.update_matter_instance, name='update_matter_instance'),
CLASS REGEX
url(r'^legal_stallion/stallion_matter_instance_update/(?P<firm_id>[0-9A-Fa-f-]+)/(?P<client_id>[0-9A-Fa-f-]+)/'
r'(?P<matter_id>[0-9A-Fa-f-]+)/$', views.UpdateMatterCreateView.as_view(), name='update_matter_instance'),
模板
<form class="cmxform form-horizontal " id="commentForm" method="post" action="#" enctype="multipart/form-data">
{% csrf_token %}
{{ form.errors }}
<div class="form-group " style="margin-bottom: 0px; margin-top: 0px">
<label for="cname" class="control-label col-lg-3">Title <span style="color: darkred">*</span></label>
<div class="col-lg-6">
{{ form.title }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Date <span style="color: darkred">*</span></label>
<div class="col-lg-3">
<div class="input-group date">
{{ form.created_date }}
<span class="input-group-addon"><i class="glyphicon glyphicon-th fa fa-calendar"></i></span>
</div>
</div>
<div class="col-sm-3">
<div class="input-group bootstrap-timepicker timepicker">
{{ form.created_time }}
<span class="input-group-addon"><i class="glyphicon glyphicon-time fa fa-clock-o"></i></span>
</div>
</div>
</div>
<div class="form-group " style="margin-bottom: 0px; margin-top: 0px; margin-bottom: 10px">
<label for="cemail" class="control-label col-lg-3">Content </label>
<div class="col-lg-8">
{{ form.content }}
</div>
</div>
<div class="form-group " style="margin-bottom: 0px; margin-top: 0px; margin-bottom: 10px">
<label for="cemail" class="control-label col-lg-3">Select Files </label>
<div class="col-lg-6">
{{ form.files }}
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-3 col-lg-6">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</div>
</form>