wtforms/flask_wtf 验证器:FileAllowed,未捕获禁止的文件扩展名
wtforms/flask_wtf validator: FileAllowed, not catching forbidden file extensions
观察到的问题
我正在尝试验证 FileField 字段的文件扩展名。即使我传递的文件的扩展名未包含在验证器构造函数中,表单也能成功验证。在此示例中,我使用的是非空白的测试 .pdf
文件:
validators=[FileRequired(),FileAllowed(['jpg', 'png'], 'Images only!')]
表单内容:
数据 = <FileStorage: 'test.pdf' ('application/pdf')>
错误 = ()
代码
路线
from flask import Flask, render_template
from forms import ConsoleUploadForm
app.config['SECRET_KEY'] = 'superdupersecret'
@app.route("/")
@app.route("/home", methods=['GET', 'POST'])
def home():
form = ConsoleUploadForm()
if form.validate_on_submit:
# this is just my debug statement
print(f"validated with -> {form.console_log_file.data} {form.console_log_file.errors}")
return render_template("home.html", form=form)
表格class
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed, FileRequired
class ConsoleUploadForm(FlaskForm):
console_log_file = FileField("Choose File...", render_kw={"class": "custom-file-input"}, validators=[
FileRequired(), FileAllowed(['jpg', 'png'], 'Images only!')])
HTML
<html>
<form method="POST" action="/home" enctype="multipart/form-data">
{{ form.hidden_tag() }}
<legend>Log Selection:</legend>
<div class="form-row">
<div class="form-group col-md-6">
<label for="console_log_file">Console Log File</label>
<div class="input-group mb-3">
<div class="custom-file">
{{ form.console_log_file(class="custom-file-input") }}
{{ form.console_log_file.label(class="custom-file-label") }}
</div>
</div>
</div>
{% if form.console_log_file.errors %}
<div class="invalid-feedback">
{% for error in form.console_log_file.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% endif %}
</div>
</form>
</html>
期望的结果
在将禁止的文件扩展名类型传递给 FileField 后按下提交,我希望在 forms.console_log_file.errors
中看到元素,并在我的调试打印语句中看到它们。
我在这里错过了什么?
这是手写错字的情况...在尝试调用 validate_on_submit 方法时遗漏了括号。
路线
if form.validate_on_submit:
---变成---> if form.validate_on_submit():
观察到的问题
我正在尝试验证 FileField 字段的文件扩展名。即使我传递的文件的扩展名未包含在验证器构造函数中,表单也能成功验证。在此示例中,我使用的是非空白的测试 .pdf
文件:
validators=[FileRequired(),FileAllowed(['jpg', 'png'], 'Images only!')]
表单内容:
数据 = <FileStorage: 'test.pdf' ('application/pdf')>
错误 = ()
代码
路线
from flask import Flask, render_template
from forms import ConsoleUploadForm
app.config['SECRET_KEY'] = 'superdupersecret'
@app.route("/")
@app.route("/home", methods=['GET', 'POST'])
def home():
form = ConsoleUploadForm()
if form.validate_on_submit:
# this is just my debug statement
print(f"validated with -> {form.console_log_file.data} {form.console_log_file.errors}")
return render_template("home.html", form=form)
表格class
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed, FileRequired
class ConsoleUploadForm(FlaskForm):
console_log_file = FileField("Choose File...", render_kw={"class": "custom-file-input"}, validators=[
FileRequired(), FileAllowed(['jpg', 'png'], 'Images only!')])
HTML
<html>
<form method="POST" action="/home" enctype="multipart/form-data">
{{ form.hidden_tag() }}
<legend>Log Selection:</legend>
<div class="form-row">
<div class="form-group col-md-6">
<label for="console_log_file">Console Log File</label>
<div class="input-group mb-3">
<div class="custom-file">
{{ form.console_log_file(class="custom-file-input") }}
{{ form.console_log_file.label(class="custom-file-label") }}
</div>
</div>
</div>
{% if form.console_log_file.errors %}
<div class="invalid-feedback">
{% for error in form.console_log_file.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% endif %}
</div>
</form>
</html>
期望的结果
在将禁止的文件扩展名类型传递给 FileField 后按下提交,我希望在 forms.console_log_file.errors
中看到元素,并在我的调试打印语句中看到它们。
我在这里错过了什么?
这是手写错字的情况...在尝试调用 validate_on_submit 方法时遗漏了括号。
路线
if form.validate_on_submit:
---变成---> if form.validate_on_submit():