On Mon, 2007-04-30 at 08:47 +0200, Mario Graziosi wrote:
> I have a form with several checkboxes created dynamically, where each 
> field has a name similar to "mycheck_xxx". From within the template I 
> don't know how many checks the form holds, since these are built 
> dynamically. How can I render them using the Django template system?
> 
> For example, this is my Form (using newforms):
> 
> class FormList(forms.Form):
>     def __init__(self, queues, *args, **kwargs):
>         super(FormList, self).__init__(*args, **kwargs)
>         # Create several check boxes
>         for n in range(5):
>             fieldname = 'mycheck_%d' % n
>             self.fields[fieldname] = forms.BooleanField(required=False)
> 
> This is the template (one of several tries). Obviously, it doesn't work, 
> but it reflects what I'd like to get on my form:
> 
> {% for fieldname in form.fields.keys %}
>     <p>{{ form.fieldname }}</p>
> {% endfor %}

Normally, once you've constructed a form class, you render it into your
template using {{ form.as_p }} or a related method. Since you've put
your extra fields into the normal self.fields attribute, the rendering
method will render those fields automatically.

If you want the presentation to change somehow, you write your own
presentation method that you call.

Alternatively, if you knew the name of the widgets, you could use
{{ form.mycheck_1 }} to do really fine-grained rendering and placement,
but if you want to loop through the list of these widgets, you will need
to have a method on your FormList class that takes a parameter and
returns the equivalent of my_form["mycheck_1"] (changing the string
based on the parameter).

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to