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: Sat 16th May, 6:00pm
"Timothy French" <tim.                   wrote:

> It's a bit hard to see what you are doing. If you are using wtforms and jinja then you are 
> definitely doing server side rendering (jinja is interpretted on the server, noit the browser. So 
> any button s in a form need to cause an action to happen, but from the wtforms point of view, the 
> only available action is to post the form, that's why there is only a submit button available.
> If you want to do client side rendering, use AJAX and jquery/DOM is the better solution.
> Does that make sense?
Sorry, server-sided rendering not client-sided

I'll try elaborate a bit

We have a quiz and each quiz has questions

The quiz is served only 1 question at a time

I'm trying to make a generic WTForm that can be passed to the template

Because the content of the WTForm is different every question - such as number of choices or what the choices are - I made a dynamic form

I made a dynamic form by making a function which returns an object of class StaticForm(FlaskForm)

The dynamic form was successfully implemented and is 100% working when I use a RadioField for the question choices:

def StyleOneForm(choices):
	class StaticForm(FlaskForm):
	StaticForm.radioField = RadioField('radioField', coerce=int, choices=choices)
	StaticForm.submit = SubmitField('Submit')
	return StaticForm()

One first implementation of the quiz utilizes radio buttons, as you can see above (RadioField)
However we also have a second implementation of the quiz so quizzes can be served in multiple styles/ways
Our second implementation of the quiz does not use radio buttons it uses regular buttons
As buttons aren't available in WTForms, I tried an array of submitFields:

def StyleTwoForm(choices):
	class StaticForm(FlaskForm):
	StaticForm.submitFields = [SubmitField(choice) for choice in choices]
	return StaticForm()

I'm having issues with this however, as when the HTML page loads I don't get a SubmitField being rendered, see attached image

Template for StyleOneForm is :
{% for subfield in form.radioField %}
							<td>{{ subfield }}</td>
							<td>{{ subfield.label }}</td>
					{% endfor %}

Template for StyleTwoForm is :
{% for submit in form.submitFields %}
                    {% endfor %}


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.
├─ THIS   Sat 16th May, 6:00pm, Nicholas T.  O.P.
├─ reply 3   Sat 16th May, 10:08pm, Timothy F.
└─ reply 4   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