我正在用 Python Flask 开发一个网站。我要做的是以其中一种形式获取上传的文件并将其附加到邮件中。我尝试了类似问题中给出的解决方案,但它给出了以下错误:
AttributeError:“str”对象没有属性“文件名”
我究竟做错了什么?我是python和flask的初学者。这是我的代码:
class UploadForm(Form):
branch = StringField('branch', validators = [DataRequired()])
year = StringField('year', validators = [DataRequired()])
sub = StringField('sub', validators = [DataRequired()])
paper = FileField('Logo', validators = [Required()])
def uploadPaper(form):
msg = Message('New upload request', sender = ADMINS[0], recipients = ADMINS)
msg.body = 'text body'
msg.html = 'Branch: ' + form.branch.data + '<br />' + 'Year: ' + form.year.data + '<br />' + 'Subject: ' + form.sub.data
msg.attach(form.paper.data.filename, 'application/octect-stream', form.paper.data.read())
with app.app_context():
mail.send(msg)
return "success"
我在验证表单时调用 uploadPaper() 函数。