Create a dynamic form using jar-wtf and sqlalchemy

I have a webapp that allows users to create their own fields for visualization in a form later.

I have a Formfield model, for example:

class Formfield(db.Model): id = db.Column(db.Integer, primary_key = True) form_id = db.Column(db.Integer, db.ForeignKey('formbooking.id')) label = db.Column(db.String(80)) placeholder_text = db.Column(db.String(80)) help_text = db.Column(db.String(500)) box_checked = db.Column(db.Boolean, nullable = True, default = False) options = db.Column(db.String) # JSON goes here? answer = db.Column(db.String) required = db.Column(db.Boolean, nullable = False, default = False) order = db.Column(db.Integer) fieldtype = db.Column(db.Integer) 

which I use to represent the field, regardless of type (checkbox, input, more in the future).

As you can see, each field has an FK for form_id.

Im trying to create a dynamic form for a given form_id. The catch is that I need to determine the type of field to render for each Formfield field. Therefore, I also need to process the field type at some point.

I assume that the solution must somehow pass the form_id to the function in my Form class.

I do not know how to do this or where to look for a solution.

Any help would be greatly appreciated!

+9
python flask flask-wtforms sqlalchemy wtforms
source share
1 answer

I think I managed to create dynamic forms with the idea from here https://groups.google.com/forum/#!topic/wtforms/cJl3aqzZieA

you need to create a dynamic form in the view function, select the form field that you want to receive, and iterate each field to create this form object. I used plain text for field types instead of integer values. Since it seems easy to read at the code level.

 class FormView(MethodView): def get(self): class DynamicForm(wtforms.Form): pass dform = main.models.Form.objects.get(name="name2") name = dform.name for f in dform.fields: print f.label setattr(DynamicForm , f.label, self.getField(f)) d = DynamicForm() # Dont forget to instantiate your new form before rendering for field in d: print field # you can see html output of fields return render_template("customform.html", form=d) def getField(self, field): if field.fieldtype == "text": return TextField(field.label) if field.fieldtype == "password": return PasswordField(field.label) # can extend if clauses at every new fieldtype 

for simple form rendering jinja template 'forms.html'

 {% macro render(form) -%} <fieldset> {% for field in form %} {% if field.type in ['CSRFTokenField', 'HiddenField'] %} {{ field() }} {% else %} <div class="form-group {% if field.errors %}error{% endif %}"> {{ field.label }} <div class="input"> {% if field.name == "body" %} {{ field(rows=10, cols=40) }} {% else %} {{ field() }} {% endif %} {% if field.errors or field.help_text %} <span class="help-inline"> {% if field.errors %} {{ field.errors|join(' ') }} {% else %} {{ field.help_text }} {% endif %} </span> {% endif %} </div> </div> {% endif %} {% endfor %} </fieldset> {% endmacro %} 

and customform.html it is

 {% extends "base.html" %} {% import "forms.html" as forms %} {% block content %} {{ forms.render(form) }} {% endblock %} 
+16
source share

All Articles