Monday, July 2, 2012

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

No comments:

Post a Comment