再次呈现包含错误的表单的技巧是什么? [英] What is the technique of presenting again form which contains error?

查看:198
本文介绍了再次呈现包含错误的表单的技巧是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Flask + Flask-Classy + WTForms,我试图实现一个路由,当用户在其输入表单中出现错误时,应用程序将再次向用户呈现该表单,并完成用户已经填充的内容。这样,用户不必担心再次输入所有的表单输入:(s)他可以纠正错误。

这些是我的班级UserView(FlaskView)的代码,这是一个瓶颈级的课程:

  def post(self):
form = forms.UserForm(request.form)
if form.validate():
if form.id.data == '':
user = models.Users()
form.populate_obj(user)
user.id = None
db.session.add(user)
else:
user = models.Users.query.get(form.id.data)
form.populate_obj(user)

db.session.commit()
return重定向(url_for( '用户视图:索引'))
else
return redirect(url_for('UserView:validation_error',error_form = form))
$ b $ def validation_error(self,error_form):
return render_template(' users / form.html',form = error_form)

你可以看到在这个尝试中,我尝试创建另一个方法: validation_error ,其中的参数 - 根据我的预期 - 应该包含所有的表单数据。但是,事实证明我得到了Jinja2异常, UndefinedError:'unicode object'没有'hidden_​​tag'的属性。当我检查它后,在 validation_error 之后, form 对象从 UserForm 转换为 unicode 对象。

有什么建议吗?或者,我试图在错误的方向上解决这个问题吗?

编辑1:详细说明如何显示错误 / p>

由于@Rawrgulmuffins增加了一个关于如何显示错误的答案。这非常漂亮,因为你可以说
$ b $ pre $ return $ render_template('users / form.html',form = form)

,如果它是第一个渲染,则获取一个无错的形式,并仅在窗体的第二个渲染上显示错误。

我不知道为什么Swdev试图重定向表单,导致它现在被操纵。我正在查看Werkzeug代码,看看我能否找到答案。


Using Flask + Flask-Classy + WTForms, I am trying to implement a route, such that, when user having error in their input form, application will presented again that form to the user, complete with what user already filled. This way, user didn't have to worried of typing again all the form input : (s)he can correct the errors.

These are code from my class UserView(FlaskView) which is a flask-classy class :

def post(self):                                                                                                                                                                                                               
    form = forms.UserForm(request.form)                                                                                                                                                                                       
    if form.validate():                                                                                                                                                                                                       
        if form.id.data=='':                                                                                                                                                                                                  
            user = models.Users()                                                                                                                                                                                             
            form.populate_obj(user)                                                                                                                                                                                           
            user.id = None                                                                                                                                                                                                    
            db.session.add(user)                                                                                                                                                                                              
        else:                                                                                                                                                                                                                 
            user = models.Users.query.get(form.id.data)                                                                                                                                                                       
            form.populate_obj(user)                                                                                                                                                                                           

        db.session.commit()                                                                                                                                                                                                   
        return redirect(url_for('UserView:index'))                                                                                                                                                                            
    else:                                                                                                                                                                                                                     
       return redirect(url_for('UserView:validation_error', error_form=form))                                                                                                                                                 

def validation_error(self, error_form):                                                                                                                                                                                       
    return render_template('users/form.html', form=error_form)       

You can see that in this attempt, I try to create another method : validation_error, with argument --that in my expectation-- should contain all the form data. But, turns out I got Jinja2 exception, UndefinedError: 'unicode object' has no attribute 'hidden_tag'. As I inspect it, after having been in validation_error, that form object is somewhat changed from UserForm into somekind of unicode object.

Any suggestion? Or, is it that I try to solve this problem in a wrong direction?

EDIT 1 : Elaborate more on how to display the error

As @Rawrgulmuffins added an answer on how to display the error inline (which I think is the best way, as the page will not get jumpy), here I pasted the code in my layout.html that list all the errors. It may not as good as an inline error display, but I think it's the easiest way there is. First I do an if for the form object, as this code is also exist for all page (I maybe refactored it even more)

 {% if form %}
                {% for error in form.errors %}
                <div class="alert alert-error">
                    <a class="close" data-dismiss="alert">×</a>
                    <strong>{{ form[error].label }} {{form.errors[error][0] }}</strong>
                </div>
 {% endfor %}
 {% endif %}

Screenshot given :

解决方案

I'm adding my answer to expand swdev's answer for anyone that doesn't understand why he can just render the template and show the errors.

Each field in a form contains a errors attribute that will be populated after validation (after form.validate()). These errors will be contained in the form object. So you can say something like,

{% if field.errors %}
    <span class="error help-inline">{{ field.errors|join(', ') }}</span>
{% endif %}

example source file. This is very nifty because you can just say

 return render_template('users/form.html', form=form) 

and if it's the first render get a error free form and display errors only on the second rendering of the form.

I'm not sure why Swdev attempt to redirect the form caused it to be manipulated right now. I'm looking through the Werkzeug code right now to see if I can find an answer.

这篇关于再次呈现包含错误的表单的技巧是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆