Python Flask-使用sendgrid API上载Wtforms文件

Python Flask-使用sendgrid API上载Wtforms文件,python,flask,sendgrid,Python,Flask,Sendgrid,我正在制作一个相对简单的表单,它有一个文件输入字段。我显然希望将上传的文件附加到通过Sendgrid python api发送的电子邮件中。表单此时已成功发送,但我没有收到任何文件上载,这是我迄今为止制作的: HTML <form action="" method="post" name="property_info" class="clearfix" enctype="multipart/form-data"> ...really long form, but here is

我正在制作一个相对简单的表单,它有一个文件输入字段。我显然希望将上传的文件附加到通过Sendgrid python api发送的电子邮件中。表单此时已成功发送,但我没有收到任何文件上载,这是我迄今为止制作的:

HTML

<form action="" method="post" name="property_info" class="clearfix" enctype="multipart/form-data">
  ...really long form, but here is the file input field
  <input type="file" id="upfile">
  ...more input fields..
</form>
excel\u email.py

app.config['UPLOAD_FOLDER'] = config.UPLOAD_FOLDER
app.config['ALLOWED_EXTENSIONS'] = config.ALLOWED_EXTENSIONS

#Check if the uploaded excel file has one of the three allowed file extensions
def allowed_file(filename):
  return '.' in filename and \
          filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


@app.route('/', methods=['GET','POST'])
def index():
  form = forms.GeneralProperty()
  year = datetime.date.today().year


if form.validate_on_submit():
    print 'form is valid'
    #upload excel file if it's present
    print request.files
      if form.excel_file.data:
      print 'there is a file uploaded'
      file = request.files['file']
      print 'file is ' + str(file)

      if file and allowed_file(file.filename):
        print 'file is allowed'
        filename = secure_filename(file.filename)
        print 'filename is ' + filename
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        #Send e-mail with attached excel file
        print 'going into email function'
        excel_email.send_mail(form, excel_file=filename)

    #Otherwise, ignore the excel file
    else:
      excel_email.send_mail(form)

    flash('Thanks for submitting the form ' + form.person_name.data.split(' ')[0] + '! We will be in contact with you shortly.', 'bg-success')

  elif form.errors:
    flash('There was an error submitting the form, please check that all required fields are filled in.', 'bg-danger')

  return render_template('index.html', form=form, year=year)
#Setting up email send method
def send_mail(form, excel_file=None):
  sg = sendgrid.SendGridClient('username', 'password')

  print 'Starting mail process....'
  message = sendgrid.Mail()
  message.add_to(str(form.person_email.data))
  message.set_from('example@heroku.com')
  message.set_from_name(str(form.person_name.data))
  message.set_subject('Property Form Submission')
  message.set_html(...really long HTML email template with all form data...)

  #If the excel file is attached, attach it to the e-mail
  if excel_file:
    message.add_attachment(excel_file)

  status, msg = sg.send(message)

  print status
  print msg
在本地主机上运行时从控制台输出

form is valid
ImmutableMultiDict([])
Starting mail process....
200
{"message":"success"}

非常感谢您的建议:)

您需要给输入一个名称。在您的视图中,您将获得
request.files['file']
,因此输入必须具有名称“file”


您需要给输入一个名称。在您的视图中,您将获得
request.files['file']
,因此输入必须具有名称“file”


您需要给输入一个名称。在您的视图中,您将获得
request.files['file']
,因此输入必须具有名称“file”


您需要给输入一个名称。在您的视图中,您将获得
request.files['file']
,因此输入必须具有名称“file”

<input type="file" name="file"/>
{{ form.file }}