5

I am using Python with Flask framework to write a microblog.

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)}}

Here is my code.

@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>

I put a flash staement inside the views and it returns correct value. If I remove (size=60) and print the form.nickname, it prints correctly. No issues. Error throws up when I have the size=60. Please let me know why error is happening.

For those of you who are interested in knowing more, I am following this tutorial , here

2 Answers 2

5

You are turning the attributes of you Field class into unicode strings

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

should be

elif request.method !="POST":
    form.nickname.data = g.user.nickname
    form.about_me.data = g.user.about_me
Sign up to request clarification or add additional context in comments.

Comments

0

It's giving you that error because nickname is not a function but a string. I can only guess that the tutorial has a few errors.

Try this instead:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.