2013-07-12 8 views
5

Sto usando Python con il framework Flask per scrivere un microblog.Errore di form Python/Flask - AttributeError: l'oggetto 'unicode' non ha attributo '__call__'

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

Ecco il mio codice.

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

Inserisco una posizione flash all'interno delle viste e restituisce il valore corretto. Se rimuovo (size=60) e stampo form.nickname, viene stampato correttamente. Senza problemi. L'errore si verifica quando ho la dimensione = 60. Per favore fatemi sapere perché sta accadendo l'errore.

Per quelli di voi che sono interessati a saperne di più, sto seguendo questo tutorial, here

risposta

5

Si sta ruotando gli attributi di voi classe Field in stringhe Unicode

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

dovrebbe essere

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

E si sta dando questo errore perché nickname non è una funzione, ma una stringa. Posso solo supporre che il tutorial abbia qualche errore.

Prova a modificare:

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