使用 Flask-Uploads 和 Flask-WTF 上传失败验证
Upload with Flask-Uploads and Flask-WTF failing validation
我似乎无法弄清楚为什么一个字段失败而另一个字段成功上传文件。我可以上传照片就好了……但是当涉及到 pdf 时,我无法让它通过验证。我已经搜索了我的代码是否有任何不妥但找不到任何东西。有趣的是我可以通过手动上传集上传照片,这对我来说没有意义。我猜我用 flask-uploads 配置 flask-wtf 的方式有些不对劲,但是看到网上没有关于如何一起使用这两者的文档,我只好尽力而为。
任何帮助将不胜感激,因为我需要上传 pdf 文件,这让我很困惑。
代码如下...非常感谢!
config.py
UPLOADED_MANUALS_DEST = "C:/pyprojects/cc/uploads/eq_manuals/"
UPLOADED_MANUALS_ALLOW = 'pdf'
UPLOADED_PHOTOS_DEST = "C:/pyprojects/cc/uploads/eq_photos/"
UPLOADED_PHOTOS_ALLOW = 'png, jpg, jpeg'
__init__.py
from flaskext.uploads import UploadSet, configure_uploads, patch_request_class
photos = UploadSet('photos')
manuals = UploadSet('manuals')
configure_uploads(app, (photos, manuals))
patch_request_class(app)
forms.py
class EQForm(Form):
name = StringField('Name', validators=[Required('Equipment Name is required.')])
photo = FileField('Equipment Photo', validators=[
FileAllowed(photos, 'Photo must be a png, jpg, or jpeg!')])
manual = FileField('Equipment Manual', validators=[
FileAllowed(manuals, 'Manual must be a pdf!')])
views.py
@app.route('/admin/equipment/', methods=['GET', 'POST'])
@roles_accepted('admin')
def equipment():
eqform = EQForm()
if eqform.validate_on_submit():
# os.path.join() the secured filename and the root path from config.py imported as join()
new_eq = Equipment(name = eqform.name.data)
if eqform.photo.data:
photo_path = join(
app.config['UPLOADED_PHOTOS_DEST'],
secure_filename(eqform.photo.data.filename)
)
eqform.photo.data.save(photo_path)
new_eq.photo_path = photo_path
if eqform.manual.data:
manual_path = join(
app.config['UPLOADED_MANUALS_DEST'],
secure_filename(eqform.manual.data.filename)
)
eqform.manual.data.save(manual_path)
new_eq.manual_path = manual_path
db.session.add(new_eq)
db.session.commit()
equipment = Equipment.query.all()
return render_template('equipment.html',
title="Equipment Database",
equipment = equipment,
eqform = eqform
)
equipment.html
<form action="" method="POST" enctype="multipart/form-data" class="eq-field hide" name="eqform">
{{ eqform.hidden_tag() }}
<ul class="list-group">
<li class="list-group-item">
{{ eqform.name.label }}: {{ eqform.name }}
</li>
<li class="list-group-item">
{{ eqform.photo.label }}: {{ eqform.photo }} <br>
<span style="font-size:.75em;">Photo must be a png, jpg, or jpeg.</span>
</li>
<li class="list-group-item">
{{ eqform.manual.label }}: {{ eqform.manual }} <br>
<span style="font-size:.75em;">Manual must be a pdf.</span>
</li>
</ul>
<input type="submit" class="btn btn-default" value="Submit">
</form>
我犯的一个愚蠢的错误:
UPLOADED_MANUALS_ALLOW = 'pdf'
UPLOADED_PHOTOS_ALLOW = 'png, jpg, jpeg'
我声明为字符串的这两个允许扩展列表...将它们更改为:
UPLOADED_MANUALS_ALLOW = ('pdf')
UPLOADED_PHOTOS_ALLOW = ('png', 'jpg', 'jpeg')
然而,有趣的是:该列表仍然无法处理单个项目,所以我不得不添加两个:
UPLOADED_MANUALS_ALLOW = ('pdf', 'PDF')
我似乎无法弄清楚为什么一个字段失败而另一个字段成功上传文件。我可以上传照片就好了……但是当涉及到 pdf 时,我无法让它通过验证。我已经搜索了我的代码是否有任何不妥但找不到任何东西。有趣的是我可以通过手动上传集上传照片,这对我来说没有意义。我猜我用 flask-uploads 配置 flask-wtf 的方式有些不对劲,但是看到网上没有关于如何一起使用这两者的文档,我只好尽力而为。
任何帮助将不胜感激,因为我需要上传 pdf 文件,这让我很困惑。
代码如下...非常感谢!
config.py
UPLOADED_MANUALS_DEST = "C:/pyprojects/cc/uploads/eq_manuals/"
UPLOADED_MANUALS_ALLOW = 'pdf'
UPLOADED_PHOTOS_DEST = "C:/pyprojects/cc/uploads/eq_photos/"
UPLOADED_PHOTOS_ALLOW = 'png, jpg, jpeg'
__init__.py
from flaskext.uploads import UploadSet, configure_uploads, patch_request_class
photos = UploadSet('photos')
manuals = UploadSet('manuals')
configure_uploads(app, (photos, manuals))
patch_request_class(app)
forms.py
class EQForm(Form):
name = StringField('Name', validators=[Required('Equipment Name is required.')])
photo = FileField('Equipment Photo', validators=[
FileAllowed(photos, 'Photo must be a png, jpg, or jpeg!')])
manual = FileField('Equipment Manual', validators=[
FileAllowed(manuals, 'Manual must be a pdf!')])
views.py
@app.route('/admin/equipment/', methods=['GET', 'POST'])
@roles_accepted('admin')
def equipment():
eqform = EQForm()
if eqform.validate_on_submit():
# os.path.join() the secured filename and the root path from config.py imported as join()
new_eq = Equipment(name = eqform.name.data)
if eqform.photo.data:
photo_path = join(
app.config['UPLOADED_PHOTOS_DEST'],
secure_filename(eqform.photo.data.filename)
)
eqform.photo.data.save(photo_path)
new_eq.photo_path = photo_path
if eqform.manual.data:
manual_path = join(
app.config['UPLOADED_MANUALS_DEST'],
secure_filename(eqform.manual.data.filename)
)
eqform.manual.data.save(manual_path)
new_eq.manual_path = manual_path
db.session.add(new_eq)
db.session.commit()
equipment = Equipment.query.all()
return render_template('equipment.html',
title="Equipment Database",
equipment = equipment,
eqform = eqform
)
equipment.html
<form action="" method="POST" enctype="multipart/form-data" class="eq-field hide" name="eqform">
{{ eqform.hidden_tag() }}
<ul class="list-group">
<li class="list-group-item">
{{ eqform.name.label }}: {{ eqform.name }}
</li>
<li class="list-group-item">
{{ eqform.photo.label }}: {{ eqform.photo }} <br>
<span style="font-size:.75em;">Photo must be a png, jpg, or jpeg.</span>
</li>
<li class="list-group-item">
{{ eqform.manual.label }}: {{ eqform.manual }} <br>
<span style="font-size:.75em;">Manual must be a pdf.</span>
</li>
</ul>
<input type="submit" class="btn btn-default" value="Submit">
</form>
我犯的一个愚蠢的错误:
UPLOADED_MANUALS_ALLOW = 'pdf'
UPLOADED_PHOTOS_ALLOW = 'png, jpg, jpeg'
我声明为字符串的这两个允许扩展列表...将它们更改为:
UPLOADED_MANUALS_ALLOW = ('pdf')
UPLOADED_PHOTOS_ALLOW = ('png', 'jpg', 'jpeg')
然而,有趣的是:该列表仍然无法处理单个项目,所以我不得不添加两个:
UPLOADED_MANUALS_ALLOW = ('pdf', 'PDF')