Russ Abbott | 2 Jul 2012 00:25
Picon
Gravatar

Newbie form question

As a Django newbie I apologize if this is a trivial question.


I'd like to use a form field as an element in a generated page but not as part of a form.  In particular, I'm generating a table, some of whose elements are text and others of which I want to be drop-down lists. I thought I might be able to use forms.ChoiceField to generate the drop-down lists.

When I write

...
choices = forms.ChoiceField([1, 2, 3]) # as a test case
...

I get a django.forms.fields.ChoiceField object. But when I include it as an element of the table nothing appears.  

Is this a reasonable approach? (It seems very easy if it really could be made to work.) If so, what should I do to get it to display as a drop-down list?

Thanks.

--
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/-/U1l9_rTtkmQJ.
To post to this group, send email to django-users <at> googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Andrea Mucci | 2 Jul 2012 11:07
Picon
Gravatar

Re: Newbie form question

hi


the approach is correct, i think the problem is with the Field name
have you used 
choices = ....... ?

if yes cloud you change the name of this field?

cheers,

2012/7/2 Russ Abbott <russ.abbott <at> gmail.com>
As a Django newbie I apologize if this is a trivial question.

I'd like to use a form field as an element in a generated page but not as part of a form.  In particular, I'm generating a table, some of whose elements are text and others of which I want to be drop-down lists. I thought I might be able to use forms.ChoiceField to generate the drop-down lists.

When I write

...
choices = forms.ChoiceField([1, 2, 3]) # as a test case
...

I get a django.forms.fields.ChoiceField object. But when I include it as an element of the table nothing appears.  

Is this a reasonable approach? (It seems very easy if it really could be made to work.) If so, what should I do to get it to display as a drop-down list?

Thanks.

--
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/-/U1l9_rTtkmQJ.
To post to this group, send email to django-users <at> googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users <at> googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Daniel Roseman | 2 Jul 2012 11:22
Picon
Gravatar

Re: Newbie form question

On Sunday, 1 July 2012 23:25:32 UTC+1, Russ Abbott wrote:

As a Django newbie I apologize if this is a trivial question.

I'd like to use a form field as an element in a generated page but not as part of a form.  In particular, I'm generating a table, some of whose elements are text and others of which I want to be drop-down lists. I thought I might be able to use forms.ChoiceField to generate the drop-down lists.

When I write

...
choices = forms.ChoiceField([1, 2, 3]) # as a test case
...

I get a django.forms.fields.ChoiceField object. But when I include it as an element of the table nothing appears.  

Is this a reasonable approach? (It seems very easy if it really could be made to work.) If so, what should I do to get it to display as a drop-down list?

Thanks.


You don't make it explicit, but I assume you're passing that 'choice' variable straight to the template, ie outside of a Form object? If so, that doesn't work. Form fields themselves don't render directly - they have to be included in a BoundField, which happens automatically when a form is initiated. You could try just declaring and instantiating a simple form containing just your fied.

Alternatively, you could try skipping the field altogether and try instantiating the widget, form.Select, directly, and calling its `render` method (it takes name and value, but value can be an empty string).
--
DR.

--
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/-/Fj7Lafn2HOQJ.
To post to this group, send email to django-users <at> googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Gmane