Skip to content

Instantly share code, notes, and snippets.

@giacomomarchioro
Created January 26, 2022 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giacomomarchioro/dd13b2db7fe8b029f5fdb5a3721ff30d to your computer and use it in GitHub Desktop.
Save giacomomarchioro/dd13b2db7fe8b029f5fdb5a3721ff30d to your computer and use it in GitHub Desktop.
Simple flask-wtf FieldList usage printing the data structure of the resulting form. Can be run using python simpleapp.py.
<form method="post" action="">
{{ form.name}}
{{ form.hidden_tag() }}
<br/>
{% for entry in form.subforms %}
{{ entry.subformfield1}}
<br/>
{% endfor %}
<input type="submit"/>
</form>
from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, FormField, FieldList, HiddenField,Form
import calendar
app = Flask(__name__)
app.secret_key = 'simpletest'
class mysubform(Form):
subformfield1 = StringField('Random text')
class myMainForm(FlaskForm):
randommaintext = StringField('Random main text')
subforms = FieldList(FormField(mysubform), min_entries=2, max_entries=7)
@app.route('/', methods=['post','get'])
def home():
form = myMainForm()
if form.validate_on_submit():
print(form.data)
#import pdb; pdb.set_trace()
return render_template('simple.html', form=form)
print(form.errors)
return render_template('simple.html', form=form)
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment