Checking if a dynamicallygenerated dropdown has a selected item

34 views
Skip to first unread message

Sithembewena Lloyd Dube

unread,
Jul 23, 2012, 4:40:08 PM7/23/12
to django...@googlegroups.com
Hi all,

I have a dynamically generated dropdown in this fashion (when viewing source):

<select name="question_3_answers">
               <option name="question_3_answer0" selected="selected"></option>
               <option name="question_3_answer_7}" value="question_3_answer_7">a) Spend the majority of time indoors</option>
               <option name="question_3_answer_8}" value="question_3_answer_8">b) Value superior darkness and consider in-car activation</option>
               <option name="question_3_answer_9}" value="question_3_answer_9">c) Value indoor clarity over outdoor darkness</option>   
</select>

<select name="question_4_answers">
               <option name="question_4_answer0" selected="selected"></option>
               <option name="question_4_answer_7}" value="question_3_answer_7">a) Spend the majority of time indoors</option>
               <option name="question_4_answer_8}" value="question_3_answer_8">b) Value superior darkness and consider in-car activation</option>
               <option name="question_4_answer_9}" value="question_3_answer_9">c) Value indoor clarity over outdoor darkness</option>   
</select>

How would one check, in a view, whether or not any of the dropdowns have a selected item? I basically need to ensure that users answer at least 3 out of 5 questions?

Thanks ...
--
Regards,
Sithembewena Lloyd Dube

Babatunde Akinyanmi

unread,
Jul 23, 2012, 5:01:27 PM7/23/12
to django...@googlegroups.com
Maybe I don't really understand the question but I think if you want
to check if the user has answered 3 questions, check from the keys in
request.GET or request.POST as the case may be.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

Thomas Orozco

unread,
Jul 23, 2012, 5:41:59 PM7/23/12
to django...@googlegroups.com

Are you using django forms?

Should be pretty straightforward once you have the input nicely formatted to just count the number of fields that have been filled in.

Sithembewena Lloyd Dube

unread,
Jul 23, 2012, 8:05:15 PM7/23/12
to django...@googlegroups.com
Hi all,

I am not using django forms for this. I did think along the lines of doing the following:
- implement the Question form, override __init__ in the model to return answers for that question,
- instantiate a form for each question, set model=whatever_model_in_current_loop and append it to a list,
- in the template, render them out

... but ...

How wuld I then access the forms collection in my view, say, from request.POST?

I think I may be trying to reinvent a wheel here.
Regards,
Sithembewena Lloyd Dube

Sithembewena Lloyd Dube

unread,
Jul 23, 2012, 8:05:54 PM7/23/12
to django...@googlegroups.com
Hi all,

I am not using django forms for this. I did think along the lines of doing the following:
- implement the Question form, override __init__ in the model to return answers for that question,
- instantiate a form for each question, set model=whatever_model_in_
current_loop and append it to a list,
- in the template, render them out

... but ...

How would I then access the forms collection in my view, say, from request.POST?


I think I may be trying to reinvent a wheel here.
On Mon, Jul 23, 2012 at 7:41 PM, Thomas Orozco <g.orozc...@gmail.com> wrote:



--
Regards,
Sithembewena Lloyd Dube

Daniel Roseman

unread,
Jul 23, 2012, 8:09:18 PM7/23/12
to django...@googlegroups.com
On Monday, 23 July 2012 21:05:54 UTC+1, Lloyd Dube wrote:
Hi all,

I am not using django forms for this. I did think along the lines of doing the following:
- implement the Question form, override __init__ in the model to return answers for that question,
- instantiate a form for each question, set model=whatever_model_in_
current_loop and append it to a list,
- in the template, render them out

... but ...

How would I then access the forms collection in my view, say, from request.POST?

I think I may be trying to reinvent a wheel here.



Yes. This is what formsets are for.
--
DR. 

Sithembewena Lloyd Dube

unread,
Jul 23, 2012, 8:13:27 PM7/23/12
to django...@googlegroups.com
Thanks Daniel :) Was just poring over the Formsets documentation now ... looks like what I need.

I will update this thread with the results later.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/1Y1zt0RYaLMJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply all
Reply to author
Forward
0 new messages