Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic default value setting for a flask form field

I am trying to set the default value for a string field in flask wtforms. The following is my code and it doesn't work.

Code:

from flask.ext.wtf import Form
from wtforms import StringField
class TestForm(Form):
     test = StringField('Test field')

@app.route('display/')
def display():
    dynamicvalue = getdynamicvalue()
    return render_template('test.html', form = form, defname = dynamicvalue)

test.html:

<div class="controls">
  {{ form.test(size=80, readonly="readonly", value={{defname}} }}
</div>

How do I correct this?

The following is the error

{{form.test(size=80, readonly= "readonly", value={{defname}}  }}
TemplateSyntaxError: expected token ':', got '}'
like image 998
pogo Avatar asked Nov 05 '15 13:11

pogo


People also ask

What is FlaskForm?

The form is the basic element that lets users interact with our web application. Flask alone doesn't do anything to help us handle forms, but the Flask-WTF extension lets us use the popular WTForms package in our Flask applications. This package makes defining forms and handling submissions easy.


1 Answers

you should use one pair of {{ }} brackets in template

<div class="controls">
  {{ form.test(size=80, readonly="readonly", value=defname) }}
</div>
like image 103
r-m-n Avatar answered Sep 19 '22 11:09

r-m-n