Faculty of Engineering and Mathematical Sciences 
Not logged in (login)


This forum is provided to promote discussion amongst students enrolled in Agile Web Development (CITS3403).
RSS cloud
Jump to:

Dynamic WTForms and multiple button/submitfields

1 of 419 articles shown, currently no other people reading this forum.
From: Nicholas T.  O.P.
Date: Sun 17th May, 12:59pm
"Timothy French" <tim.                   wrote:

> I'm not sure what's happening there, but the hack of using a submit field for a button looks a bit dubious.
> The 
> {{submit}}
> will just render 
> submit.__repr__()
> into the HTML, so maybe use Flask shell to investigate what's happening there.
>  From what I can tell, the Unbound form reference comes from the _form attribute not being instantiated. I'm guessing this is because each 
> form should only have one submit field? 

Agreed it looks hacky but as far as I can see there's no other way to do a button using WTForms?
Some people have used multiple submit fields for different functions:
Dynamic forms:

As the dynamic form with RadioField instantiates correctly I assume that the problem is with arrays of fields don't get instantiated correctly 
by WTForms
The following renders perfectly fine:
def StyleTwoForm(choices, *args, **kwargs):
	class StaticForm(FlaskForm):
	#StaticForm.submitFields = [SubmitField(choice[1]) for choice in choices]
	StaticForm.submitField1 = SubmitField(choices[0])
	StaticForm.submitField2 = SubmitField(choices[1])
	return StaticForm()

There is a FieldList, however this doesn't support SubmitField:
class wtforms.fields.FieldList

I realize that this is becoming a very specific question, I don't have time to focus on this much more as I've already spent too much time on 
this single piece of functionality and will just put something hacky in to make do so I can move on to more important functionality, I've done 
the following:

def StyleTwoForm(choices, *args, **kwargs):
	class StaticForm(FlaskForm):
	#StaticForm.submitFields = [SubmitField(choice[1]) for choice in choices]
	StaticForm.len = len(choices)
	if StaticForm.len > 0: StaticForm.submitField1 = SubmitField(choices[0][1])
	if StaticForm.len > 1: StaticForm.submitField2 = SubmitField(choices[1][1])
	... etc
	return StaticForm()

{% if form.len > 0 %}
                            {% endif %}
                            {% if form.len > 1 %}
                            {% endif %}
                            {% if form.len > 2 %}
                            {% endif %}
                            ... etc


Related articles

Dynamic WTForms and multiple button/submitfields (all 5) RSS
├─ original   Sat 16th May, 2:33pm, Nicholas T.
├─ reply 1   Sat 16th May, 3:54pm, Timothy F.
├─ reply 2   Sat 16th May, 6:00pm, Nicholas T.  O.P.
├─ reply 3   Sat 16th May, 10:08pm, Timothy F.
└─ THIS   Sun 17th May, 12:59pm, Nicholas T.  O.P.
This Page

Program written by: [email protected]
Feedback welcome
Last modified:  8:27am May 24 2020