7

下面显示了我的示例项目。但它在提交时引发错误。我已将 enctype 添加到表单字段。但没办法。请帮忙 :)

html

<form class="forms2" action="." method="post" enctype="multipart/form-data">{%csrf_token%}
            <fieldset>
              <ol>

                   {% for field in forms %}
                   <li class="form-row">

                  <b>{{ field.label_tag }}</b> {{ field }} </li>
                  <i>{{ field.errors }}</i>


                  {%endfor%}



                  <li class="button-row">
                  <input type="submit" value="Submit" name="submit" class="btn-submit" />
                </li>
              </ol>

              </fieldset>

                </form>

表格.py

class CarrerApplication(forms.Form):
    applicant_name = forms.CharField(widget=forms.TextInput(attrs={'class':'text-input required'}),label='Name')
    applicant_address = forms.CharField(widget=forms.TextInput(attrs={'class':'text-input required'}),label='Address')
    applicant_email = forms.EmailField(widget=forms.TextInput(attrs={'class':'text-input required email'}),label='Email')
    applicant_mobile = forms.CharField(widget=forms.TextInput(attrs={'class':'text-input'}),label='Mobile')
    applicant_resume = forms.FileField(widget=forms.FileInput(attrs={'name':'applicant_resume'}),label='Resume')
    applicant_message = forms.CharField(widget=forms.Textarea(attrs={'class':'text-area'}),label='Message')



    def clean_applicant_resume(self):
        applicant_resume = self.cleaned_data['applicant_resume']
        exts = ['doc', 'docx', 'pdf', 'jpg', 'png', 'xls', 'xlsx', '.xlsm', '.xlsb']
        max_email_attach_size = 10485760 #10MB written in bytes

        if not applicant_resume in (None, ''):

            try:
                if applicant_resume.size > max_email_attach_size:
                    raise ValidationError("The file is too big")

                file_extension = applicant_resume.name.split('.')[1]
                if file_extension not in exts:
                    raise ValidationError("Invalid File Type")

            except AttributeError:
                raise ValidationError(self.error_messages['invalid'])

        return applicant_resume

视图.py

def careers(request):



   if request.method == 'POST':

      form = CarrerApplication(request.POST,request.FILES)
      if form.is_valid():

            applicant_name= request.POST.get('applicant_name')
            applicant_address=request.POST.get('applicant_address')
            applicant_mobile=request.POST.get('applicant_mobile')
            applicant_email=request.POST.get('applicant_email')
            applicant_resume=request.FILES.get('applicant_resume')
            applicant_message = request.POST.get('applicant_message')
            subject = 'Resume from Blaah blaah'
            email = 'anishmenon@me.com'

            message_template = loader.get_template('resume_mail/resume_send.txt')
            message_context = Context({ 'name': applicant_name,'message':applicant_message })
            message = message_template.render(message_context)
            mail = EmailMessage(subject, message, applicant_email, [email])

            mail.attach(applicant_resume.name, applicant_resume.read(), applicant_resume.content_type)
            mail.send()

            return HttpResponse('Thanks')
   else:
        form = CarrerApplication()

   return render_to_response('resume_mail/resume_apply.html',
                           {'forms' : form},
                              context_instance=RequestContext(request))

我在提交表单时遇到错误

没有提交文件。检查表单上的编码类型。

4

3 回答 3

1

您需要使用request.POST和实例化您的表单request.FILES

form = CarrerApplication(request.POST, request.FILES)

有关更多信息,请参阅有关将上传的文件绑定到表单的文档。

于 2013-06-05T10:39:19.260 回答
1

这样做会很酷

form = CarrerApplication((request.POST,request.FILES) or None)
if request.method == 'POST':
     .......
于 2013-06-05T15:17:53.780 回答
0

我也遇到了这个问题,我把 FileInput 改成 ClearableFileInput。

applicant_resume = forms.FileField(widget=forms.ClearableFileInput(attrs={'name':'applicant_resume'}),label='Resume')
于 2020-07-12T16:15:08.167 回答