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

help3403


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

Dynamic WTForms and multiple button/submitfields

1 of 419 articles shown, currently no other people reading this forum.
photo
From: Nicholas T.  O.P.
Date: Sun 17th May, 12:59pm
Actions: 
        Login-to-reply
"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:
https://stackoverflow.com/questions/35774060/determine-which-wtforms-button-was-pressed-in-a-flask-view
Dynamic forms:
https://stackoverflow.com/questions/51599936/how-do-i-bind-an-flask-wtform-unboundfield

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):
		pass
	#StaticForm.submitFields = [SubmitField(choice[1]) for choice in choices]
	StaticForm.submitField1 = SubmitField(choices[0])
	StaticForm.submitField2 = SubmitField(choices[1])
	..etc..
	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:

Form:
def StyleTwoForm(choices, *args, **kwargs):
	class StaticForm(FlaskForm):
		pass
	#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()

Template:
{% if form.len > 0 %}
                                {{form.submitField0()}}
                            {% endif %}
                            {% if form.len > 1 %}
                                {{form.submitField1()}}
                            {% endif %}
                            {% if form.len > 2 %}
                                {{form.submitField2()}}
                            {% endif %}
                            ... etc


Thanks

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