Python / Flask表单错误 - AttributeError:'unicode'对象没有属性'__call__'

时间:2013-07-12 23:41:01

标签: python flask

我正在使用带有Flask框架的Python来编写微博。

File "/home/akoppad/flaskblog/app/templates/base.html", line 27, in top-level template code
{% block content %}{% endblock %}
File "/home/akoppad/flaskblog/app/templates/edit.html", line 13, in block "content"
[Display the sourcecode for this frame]  [Open an interactive python shell in this frame]
{{form.nickname(size=60)}}

这是我的代码。

@app.route('/edit', methods=['GET','POST'])
@login_required
def edit():
    form=EditForm(g.user.nickname)
    if form.validate_on_submit():
            g.user.nickname = form.nickname.data
            g.user.about_me = form.about_me.data
            db.session.add(g.user)
            db.session.commit()
            flash('Your changes have been saved.')
            return redirect_url(url_for('edit'))
    elif request.method !="POST":
            form.nickname = g.user.nickname
            form.about_me = g.user.about_me
    else:
            form.nickname.data = g.user.nickname
            form.about_me.data = g.user.about_me
    flash(form.nickname)
    flash("inside edit")
    return render_template('edit.html', form=form)


<form action="" method="post" name="edit">
{{form.hidden_tag()}}
<table>
    <tr>
        <td>Your nickname:</td>
        <td>
            {{form.nickname(size = 24)}}
            {% for error in form.errors.nickname %}
            <br><span style="color: red;">[{{error}}]</span>
            {% endfor %}
        </td>
    </tr>
    <tr>
        <td>About yourself:</td>
        <td>{{form.about_me(cols = 32, rows = 4)}}</td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" value="Save Changes"></td>
    </tr>
</table>
</form>

我在视图中放了一个flash staement,它返回正确的值。如果我删除(size=60)并打印form.nickname,则会正确打印。没有问题。当我有大小= 60时错误抛出。请让我知道为什么会发生错误。

对于那些有兴趣了解更多内容的人,我正在关注本教程here

2 个答案:

答案 0 :(得分:5)

您正在将Field类的属性转换为unicode字符串

elif request.method !="POST":
    form.nickname = g.user.nickname
    form.about_me = g.user.about_me

应该是

elif request.method !="POST":
    form.nickname.data = g.user.nickname
    form.about_me.data = g.user.about_me

答案 1 :(得分:0)

它给你的错误是因为nickname不是函数而是字符串。我只能猜测教程有一些错误。

请改为尝试:

{{ form.nickname|truncate(60) }}