单击烧瓶中的提交没有任何反应(FlaskForm)
Nothing is happening on clicking Submit in flask (FlaskForm)
这是我的代码(我更改了它以检查表单是否有效):
观看功能:
@app.route('/modi',methods=['GET','POST'])
@login_required
def IssMed():
form = medis()
if form.validate_on_submit():
flash("Ok")
redirect(url_for('ViewMedicine'))
return render_template('issuemedicine.html',form=form)
我的forms.py:
class medis(FlaskForm):
name = StringField("Medicine Name",validators=[Optional()])
qty = IntegerField("Quantity",validators=[Optional()])
submit = SubmitField('Issue')
我的html文件:
{% extends "base.html" %}
{% block content %}
<form method="post">
{{form.name.label}} {{form.name()}}
{{form.qty.label()}} {{form.qty()}}
{{form.submit()}}
</form>
{% endblock %}
我无法确定我在哪里犯了错误,请帮助我:(
两件事。
您没有看到任何错误,因为您没有打印任何错误。如果表单尚未提交,打印错误列表可能是有意义的。有多种方法可以做到这一点,例如在每个字段上打印错误,或在表单顶部打印错误列表:
{% for error_field, error_list in form.errors.items() %}
<p>Errors on field: {{error_field}}</p>
<ul>
{% for error in error_list %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endfor %}
其次,原因很可能是您缺少 csrf 令牌。将此添加到您的表单中:
{{ form.csrf_token }}
有关详细信息,请查看 Form Validation docs。
这是我的代码(我更改了它以检查表单是否有效):
观看功能:
@app.route('/modi',methods=['GET','POST'])
@login_required
def IssMed():
form = medis()
if form.validate_on_submit():
flash("Ok")
redirect(url_for('ViewMedicine'))
return render_template('issuemedicine.html',form=form)
我的forms.py:
class medis(FlaskForm):
name = StringField("Medicine Name",validators=[Optional()])
qty = IntegerField("Quantity",validators=[Optional()])
submit = SubmitField('Issue')
我的html文件:
{% extends "base.html" %}
{% block content %}
<form method="post">
{{form.name.label}} {{form.name()}}
{{form.qty.label()}} {{form.qty()}}
{{form.submit()}}
</form>
{% endblock %}
我无法确定我在哪里犯了错误,请帮助我:(
两件事。
您没有看到任何错误,因为您没有打印任何错误。如果表单尚未提交,打印错误列表可能是有意义的。有多种方法可以做到这一点,例如在每个字段上打印错误,或在表单顶部打印错误列表:
{% for error_field, error_list in form.errors.items() %}
<p>Errors on field: {{error_field}}</p>
<ul>
{% for error in error_list %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endfor %}
其次,原因很可能是您缺少 csrf 令牌。将此添加到您的表单中:
{{ form.csrf_token }}
有关详细信息,请查看 Form Validation docs。