我試圖在我的表單上返回自定義錯(cuò)誤消息,但由于某種原因,該錯(cuò)誤在重新加載時(shí)沒有顯示在表單上。我猜 field.error 對(duì)象是空的,但我不知道為什么。我返回錯(cuò)誤的方式就像在文檔中一樣。 def validate_email(self, email): user = models.User.query.filter_by(email=email.data).first() if user is None: raise ValidationError('Such user not found')這是我的 render_template 宏:{% macro render_field(field) %} {{ field.label }} {{ field }} {% if field.errors %} <ul> {% for error in field.errors %} <li>{{ error }}</li> {% endfor %} </ul> {% endif %}{% endmacro %}
1 回答

慕蓋茨4494581
TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
我的問題在于我如何重新呈現(xiàn)模板,我使用的是表單類,而不是剛剛使用的特定表單對(duì)象。
return render_template(self.template_name, title=self.title, form=form_class())
解決方案
return render_template(self.template_name, title=self.title, form=form)
編輯,但這并沒有解決我的自定義異常 LoginError 未被計(jì)為字段錯(cuò)誤的問題
添加回答
舉報(bào)
0/150
提交
取消