Re: Improve the dynamic generation of a form with newforms library

2007-07-16 Thread Russell Keith-Magee

On 7/16/07, yml <[EMAIL PROTECTED]> wrote:
>
>  This is not bat but i would prefer if instead of the list of tuple
> containing my forms could directly instantiate a "all in one" form.
>  This would avoid the loop on list_answer_forms. This is were I get
> stuck. Could you please let me know how to add html text between the
> field?
>  like "{{ poll.title }} | {{ poll.question }}"

If you want to do this, you will need to add a custom rendering scheme
to your form.

When you render a form using {{ form }}, you are effectively making a
call to form.as_table(). Django also provides an as_p and as_ul
variant to modify the way individual fields are rendered, which can be
accessed as {{ form.as_p }} or {{ form.as_ul }}.

To get per-field rendering like you describe, you will need to add a
custom as_ method. Look at the implementation of
django.newforms.Form.as_p() for an example implementation; adapting to
match your needs shouldn't be too hard.

Yours,
Russ Magee %-)

--~--~-~--~~~---~--~~
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@googlegroups.com
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
-~--~~~~--~~--~--~---



Improve the dynamic generation of a form with newforms library

2007-07-16 Thread yml

Hello Djangonauts,

After spending sometimes to learn using the test/fail/correct cycle I
have finally reach a point where I have something that more or less do
what I want. However since one of the objectives of the project I am
working on was to improve my knowledge on django I would like to read
from you how to improve what I have done.

The goal is to generate dynamically a form using the newforms
library.
This forms should all the user to create an Interview
(models.py:http://dpaste.com/hold/14560/ )
What I have done so far is create this custom form:

class AnswerPollForm(forms.Form):
def __init__(self, poll_id, *args, **kwargs):
super(AnswerPollForm, self).__init__(*args, **kwargs)
self.poll = Poll.objects.get(id=poll_id)
for c in self.poll.choice_set.iterator():
self.fields[c.id] =
forms.BooleanField(label=c.choice,required=False)

  That I use in this view (http://dpaste.com/hold/14562/). The
interesting part is that I create a list of tuple made with the polls
and the forms representing them. This allow  me to create a single
form that I can use to collect the interview to a survey.

list_answer_forms =[]
for poll in survey.poll_set.iterator():
list_answer_forms.append((poll,AnswerPollForm(poll.id)))
[...]
return render_to_response('dj_survey/template/
add_interview_forms.html',
  {'list_answer_forms':
list_answer_forms,
  'survey': survey,
  'request': request})

This list of forms is then used in the following template:
Welcome to {{ survey.name }}
{{ survey.description }} 

{% for poll,form in list_answer_forms %}
{{ poll.title }} | {{ poll.question }}

{{ form.as_ul }}

{% endfor %}
 



 This is not bat but i would prefer if instead of the list of tuple
containing my forms could directly instantiate a "all in one" form.
 This would avoid the loop on list_answer_forms. This is were I get
stuck. Could you please let me know how to add html text between the
field?
 like "{{ poll.title }} | {{ poll.question }}"

 Any other improvement is more than welcome.
 Thank you for your time.

 Regards,


--~--~-~--~~~---~--~~
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@googlegroups.com
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
-~--~~~~--~~--~--~---