即使不需要,Django文件字段更新也会导致错误 [英] Django file field update causing error even though not required

查看:151
本文介绍了即使不需要,Django文件字段更新也会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序来更新模型的某些字段。有4个可能的字段可以更新:resolution,upload4,upload5和upload6。上传字段不是必需的。如果我不包括request.FILES行,上传的文件将不会保存到数据库中,但似乎是因为我已经包含它,我需要总是上传3个文件,即使它们不是必需的。我得到的例外是POST上的MultiValueDictKeyError。如何解决这个问题?我想要添加3个文件的选项,但我不想每次都需要。我了解如何使一个字段不需要,我不知道如何编写请求.FILES了解它不是必需的。



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆