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:

project 2 - flask forms

1 of 419 articles shown, currently no other people reading this forum.
photo
From: Haolin W.
Date: Mon 11th May 2020, 10:39pm
Actions: 
        Login-to-reply

 

Hey there. Your structure for the answers table sounds correct. My understanding is that you 
want multiple questions on the same page, and the ability to answer multiple questions in a 
single POST correct? 

If this is what you're intending to do, there's many ways you can do it. The best way in my 
opinion with the best user experience is to tackle this problem with AJAX, and have all the 
questions on the same page, and post each answer with each button. This allows you to do 
questions, and come back later and complete them. However, based on your structure, what you can 
do is AJAX an array of answers, or a JSON object with key value pairs, where the key is the 
primary key of the question, and the answer is the answer. Then, in the controller, loop over 
the JSON object that's posted, and create new Answer objects from that.

If you want this to be a purely WTForm based solution, the solution is much more complicated, as 
you don't have the ability to POST a custom JSON object, you kind of have to deal with it as a 
form. What you can do is ignore the default form validation on your WTForm, and write custom 
validation. i.e. if a certain form field isn't null, and if you have the question id, create the 
answer object with the correct relationships. This would mean that the <form> tag envelopes 
every form field on the page, so that once you POST, you POST every form field on the page 
rather than specific fields. To achieve this, you need to instead of using WTForms, do something 
like:

<form method=POST>
> {% for questions in question_row %}
>     <tr>
>         <td>
>             <div class="form-group">
>             <label id={{questions.question}} name=thisQuiz>{{questions.question}}</label>
>             </div>
>             
>         </td>
>         <td>
>             {{form.answer}}
>         </td>
>     {% endfor %}
> <button type='submit'>Submit</button>
> 
> </form>

Which allows you to post everything. I highly suggest against this, as in the back end the 
processing would be way more complicated (in my opinion anyway) and to solve the issue with AJAX 
instead, as posting a JSON object or even a JavaScript array would be so much easier to work 
with.

Let me know how you go, you can email me at hao*i*.*[email protected]*a*e*u*au or contact me via discord at 
haolin#2060.

ANONYMOUS wrote:

> Hi there, 
> 
> I have added the questions for a particular quiz to the fill out quiz html page like this:
> <tbody>
>     {% for questions in question_row %}
>     <tr>
>         <td>
>             <div class="form-group">
>             <label id={{questions.question}} name=thisQuiz>{{questions.question}}</label>
>             </div>
>             
>         </td>
>         <td>
>             {{form.answer}}
>         </td>
>     {% endfor %}
>     </tr>
> </tbody>   
> </table>
> 
> <div class="form-group" style="text-align: center;">
>     {{ form.submit(class="btn btn-secondary ml-3") }}
> 
> 
> 
> The answers table in the db is set up so that each row represents an answer for a 
> particular question for a particular student.
> Is there a way for me to loop through the form answers in the controller, so that on submit 
> i can add multiple rows of data to the answers table, so that I can add each answer to each 
> question for the student filling out the quiz to the db.
> Very sorry if this doesn't make sense 
> 
> Thank you

Related articles

project 2 - flask forms (all 3) RSS
├─ original   Mon 11th May 2020, 11:51am, ANONYMOUS
├─ THIS   Mon 11th May 2020, 10:39pm, Haolin W.
└─ reply 2   Tue 12th May 2020, 10:05am, Timothy F.
This Page


Program written by: [email protected]
Feedback welcome
Last modified: 11:27am Sep 21 2020