即使不需要,Django文件字段更新也会导致错误 [英] Django file field update causing error even though not required
问题描述
views.py
@login_required(login_url ='/ login /')
def report(request,case_id):
form = ReportForm()
case = get_object_or_404(事件,pk = case_id)
#如果这是一个POST请求,我们需要处理表单数据
如果request.POST:
#创建一个表单实例,并使用请求中的数据填充它:
form = ReportForm(request.POST)
如果form.is_valid():
resolution =(form.cleaned_data ['resolution'])#从用户输入抓取action_taken
case .resolution = resolution
case.upload4 = request.FILES ['upload4']
case.upload5 = request.FILES ['upload5']
case.upload6 = request.FILES ['upload6 ']
case.status = Status.objects.get(status ='closed')
case.save(update_fields = ['resolution','status'上传4','upload5','upload6'])
context = {'case':case,
'form':form}
return HttpResponseRedirect(reverse('dashboard_app:dashboard' )
template =report.html
#form = CaseForm()
context = {'case':case,
'form':form}
返回渲染(请求,模板,上下文)
重点是您忽略表单的验证,并直接返回请求中的数据。所以,是的,如果表格不存在,那将会破裂。但是这正是我们使用表单的原因。
case.upload4 = form.cleaned_data ['upload4']
等。
如果您使用的ModelForm;那么您可以通过 case
作为表单的实例
参数,只需执行表单。 save()
,替换 is_valid
块中的几乎所有代码。
I have an app that serves to update certain fields of a model. There are 4 possible fields that could be updated: resolution, upload4, upload5, and upload6. The upload fields are NOT required. If I do not include the request.FILES line, the uploaded file will not be saved to the database, but it seems like because I've included it, I need to always upload the 3 files, even though they are not required. The exception I am getting is "MultiValueDictKeyError" on the POST. How can I fix this? I want the option to add 3 files, but I don't want to have to every time. I understand how to make a field not required, I don't know how to code the request.FILES to understand that it is not required.
views.py
@login_required(login_url='/login/')
def report(request, case_id):
form = ReportForm()
case = get_object_or_404(Incident, pk=case_id)
# if this is a POST request we need to process the form data
if request.POST:
# create a form instance and populate it with the data from the request:
form = ReportForm(request.POST)
if form.is_valid():
resolution = (form.cleaned_data['resolution']) # grabbing action_taken from user input
case.resolution = resolution
case.upload4 = request.FILES['upload4']
case.upload5 = request.FILES['upload5']
case.upload6 = request.FILES['upload6']
case.status = Status.objects.get(status='closed')
case.save(update_fields=['resolution', 'status', 'upload4', 'upload5', 'upload6'])
context = { 'case': case,
'form': form}
return HttpResponseRedirect(reverse('dashboard_app:dashboard'))
template = "report.html"
#form = CaseForm()
context = { 'case': case,
'form': form}
return render(request, template, context)
The point is that you are ignoring the validation that form does, and going straight back to the data from the request. So, yes, that will break if the forms are not there. But this is exactly why we use forms.
case.upload4 = form.cleaned_data['upload4']
etc.
It would be even easier if you used a ModelForm; then you could pass case
as the instance
argument of the form, and just do form.save()
, replacing almost all the code inside your is_valid
block.
这篇关于即使不需要,Django文件字段更新也会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!