MultiValueDictKeyError:文件附件

时间:2020-09-16 00:18:46

标签: django debugging django-views

您好,我正在尝试通过表单从附件发送带有文件的电子邮件,但出现MultiValueDictKeyError错误 这是我的观点:

def postuler_save(request):
    name = request.POST.get('name', '')
    mail = request.POST.get('mail','')
    numero = request.POST.get('numero','')
    etablissement = request.POST.get('etablissement','')
    email= EmailMessage("message from:"  + "" + name, "phone number:" + ""+ numero + "mail:" + ""+ mail+"etablissement" + ""+ etablissement,EMAIL_HOST_USER,[mail])
    email.content_subtype = 'html'
    file = request.FILES['file']
    email.attach(file.name, file.read(), file.content_type)
    email.send()
    return HttpResponseRedirect("/")

这是错误的回溯

During handling of the above exception ('file'), another exception occurred:
  File "C:\Users\WD\Miniconda3\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\WD\Miniconda3\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\WD\Desktop\weserveit\gestion\views.py", line 32, in postuler_save
    file = request.FILES['file']
  File "C:\Users\WD\Miniconda3\lib\site-packages\django\utils\datastructures.py", line 78, in __getitem__
    raise MultiValueDictKeyError(key)

Exception Type: MultiValueDictKeyError at /postuler_save
Exception Value: 'file'

这是我的html文件

 <form action="/postuler_save" method="post" enctype="multipart/form-data">
                             {% csrf_token %}
                        <h3 class="register-heading">Postuler pour un emploi</h3>
                        <div class="row register-form">
                            <div class="col-md-6">

                                <div class="form-group">
                                    <input type="text" class="form-control" placeholder=" *" id="name" />
                                </div>
                                <div class="form-group">
                                    <input type="email" class="form-control" placeholder=" *" id="mail" />
                                </div>
                            </div>
                                <div class="form-group">
                                    <input type="number" name="txtEmpPhone" class="form-control" placeholder=" *" id="numero" />
                                </div>
                                <div class="form-group">
                                    <input type="file" class="form-control"  id="file" />
                                </div>
                                <input type="submit" class="btnRegister"  value="Je postule"/>
                            </div>
                        </div>
                    </form>

1 个答案:

答案 0 :(得分:1)

这是在告诉您file中没有名为request.FILES的对象。您需要从Web表单显示从POSTpostuler_save的代码。

确保在enctype中设置了form

<form action="/postuler_save" method="post" enctype="multipart/form-data">
    <input type="file" id="file" name="file">

要检查请求中的内容:

for filename, file in request.FILES.iteritems():
    print(filename, file)
相关问题