List Info

Thread: Re: How to render newforms dynamically created fields?




Re: How to render newforms dynamically created fields?
country flaguser name
United States
2007-04-30 15:10:53
Two thoughts on this:
First: you could define an appropriate label for the
dynamic
formfields as you create them in your FormList class (not
shown here)
such as the name of the thing that would be deleted, and
then use the
as_p shortcut to render the whole form rather than trying to
loop:

with a view like:

def form_list_view(request):
    my_form = FormList(queues=[])
    return
render_to_response('formlist.html',{'form':my_form})

then within the <form> tags of template
formlist.html:

{{ form.as_p }}

If you aren't happy with that approach, then you are in for
an ugly
hack, such as a view like:

def form_list_view(request):
    my_form = FormList(queues=[])
    my_fields = ['%s: %s' % (field.label or
name,field.widget.render(name,'')) for name,field in
my_form.fields.items()]
    return
render_to_response('formlist.html',{'my_fields':my_fields})

and template code like:

{% for field in my_fields %}
<p>{}</p>
{% endfor %}

basically hacking out code like in
django.newforms.forms.BaseForm._html_output

On Apr 30, 2:47 am, Mario Graziosi <mgrazi...fgasoftware.com> 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 %}
>
> Does any one know how I might accomplish this?
>
> Thanks in advance for any help.
>
> --
> Mario Graziosi, mailto:mgrazi...fgasoftware.com
> FG&A srl (http://www.fgasoftware.co
m/)
> Voicework: The agile PBX (http://www.voiceatwork.eu/
)
> Tel: 02 9350-4780 interno 101, Fax: 02 9139-0172


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


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )