I'm collaborating with Drise on this project.
This is driving me up the wall. I took at the survey app (Thanks Bruno
and Massimo!)
Here's what i've got:
def take():
quiz = db().select(db.quiz.ALL).first()
questions=db(db.question.quiz_id==quiz.id).select()
answer_value=0
for question in questions:
options=[question['option_'+c] for c in ['A','B','C','D'] if
question['option_'+c].strip()]
stuff=TABLE(*[TR(INPUT(_type="radio",_name="value",_value=str(i),
value=answer_value),item) for i,item in enumerate(options)])
form=FORM(H2('Your answer'),stuff,BR(),BR(),
INPUT(_type='submit',
_value=str(T('Submit answer'))
)
)
return dict(questions=questions,stuff=stuff,form=form)
When I pull this up in the browser, it works just fine, but only for
the last question in the database. It appears that "for question in
questions:" is not looping. There are multiple questions in the
database. Also, I can manually specify question=questions[0] or
question=questions[1] and it pulls up the respective questions.
On Apr 15, 2:38 pm, pbreit <[email protected]> wrote:
> You probably need 'answers' to reference auth_user or come up with a scheme
> to track the person answering.
>
> Then, select() a question and build a form. Perhaps:
>
> question = db.question(1) #or however you want to pick the question
> form = SQLFORM(db.answers)
> form.vars.question_id = question.id
> return dict(question=question, form=form)
>
> To hide the form elements you don't want, you might need to either customize
> the form or set readable/writable.
>
> Q: {{=question.question}}<br>
> A: {{=form}}