-
-
Save doobeh/4667330 to your computer and use it in GitHub Desktop.
<form method="post"> | |
{{ form.hidden_tag() }} | |
{{ form.example }} | |
<input type="submit"> | |
</form> |
from flask import Flask, render_template | |
from flask.ext.wtf import Form, RadioField | |
SECRET_KEY = 'development' | |
app = Flask(__name__) | |
app.config.from_object(__name__) | |
class SimpleForm(Form): | |
example = RadioField('Label', choices=[('value','description'),('value_two','whatever')]) | |
@app.route('/',methods=['post','get']) | |
def hello_world(): | |
form = SimpleForm() | |
if form.validate_on_submit(): | |
print form.example.data | |
else: | |
print form.errors | |
return render_template('example.html',form=form) | |
if __name__ == '__main__': | |
app.run(debug=True) |
something wrong happened in my app, but it goes well when i set
example = RadioField('Label', coerce=int, choices=[('value','description'),('value_two','whatever')])
.
Nice simple example. Can you say how you could modify this example so that it did not have a "submit" button, but that each radio button press did a POST? I am guessing that I would need to construct my own widget (or overload ListWidget) and populate with RadioInput and implement all the semantics of radio buttons.
nice, I have been looking for this
Great !!!
What I need clear:
- I have form "role.html" and I am using :
{{ wtf.form_field(form.jobtitle, placeholder='Job Title', size=10 ) }} | {{ wtf.form_field(form.jobtype) }} | {{ wtf.form_field(form.companytype) }} | {{ wtf.form_field(form.companyculture) }} |
To generate.
Now, when I will enter the values accordingly, how can I show them on webpage (on label) ?
Kindly guide, as I am trying to learn Python/Flask
This is how it worked for me under python 3.
from flask import Flask, render_template
from wtforms import Form, RadioField
SECRET_KEY = 'development'
app = Flask(__name__)
app.config.from_object(__name__)
class SimpleForm(Form):
example = RadioField(
'Label', choices=[('value', 'description'), ('value_two', 'whatever')])
@app.route('/', methods=['post', 'get'])
def hello_world():
form = SimpleForm()
if form.validate():
print(form.example.data)
else:
print(form.errors)
return render_template('example.html', form=form)
if __name__ == '__main__':
app.run(debug=True)
I also needed to remove the hidden_tag()
from the form, since I get the error message: object has no attribute 'hidden_tag'
.
<form method="post">
{{ form.example }}
<input type="submit">
</form>
This was what I had to do to get it working on Python 3 using FlaskForm
from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import RadioField
SECRET_KEY = 'development'
app = Flask(__name__)
app.config.from_object(__name__)
class SimpleForm(FlaskForm):
example = RadioField('Label', choices=[
(1,'description'), (2,'whatever')],
default=1, coerce=int)
@app.route('/',methods=['post','get'])
def hello_world():
form = SimpleForm()
if form.validate_on_submit():
print(form.example.data)
else:
print(form.errors)
return render_template('example.html',form=form)
if __name__ == '__main__':
app.run(debug=True)
this works
how to validate the radiobutton
how to make the options side by side, like this:
- option 1 *option 2 *option3 * option4 ...
how to validate the radiobutton
from wtforms.validators import InputRequired
example = RadioField('Label', choices=[
(1,'description'), (2,'whatever')],
default=1, coerce=int, validators=[InputRequired()])
For anyone else who stumbles across this gist as a simple flask forms example, here is what you need to know to get this running. Instruction syntax is specific for unix or macos.
You will need to have some python modules installed,
The forms extension for the flask package has been reorganized since this gist was created.
RadioField
should be imported fromwtforms
, notflask.ext.wtf
. The import at the top ofsiecje.py
should look like this,Flask looks for the template file in the
templates/
subdirectory, so move theexample.html
file there.When you run the app open a browser on http://127.0.0.1:5000/ to see the form.