List Info

Thread: newforms tricks




newforms tricks
user name
2007-12-28 22:25:33
After wrestling with newforms to get it to do things I
thought it was
never intended to do, I decided that either it was really
intended to
do such things or it's just such a clean design that getting
it to do
weird things isn't that hard.

That said, I spent a lot of time fiddling around to find the
right
incantations, so I added a section to the Cookbook[1]
called
newforms[2] and included instructions for creating form
fields
dynamically[3] and altering the default order[4]. If anyone
sees any
obvious stupidness or this duplicates information available
elsewhere,
please let me know.

Todd

[1]: http://co
de.djangoproject.com/wiki/CookBook
[2]: h
ttp://code.djangoproject.com/wiki/CookBookNewForms
[3]: http://code.djangoproject.com/wiki/CookBookNewFo
rmsDynamicFields
[4]: http://code.djangoproject.com/wiki/CookBookNewFo
rmsDynamicFields

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---


Re: newforms tricks
country flaguser name
France
2007-12-29 03:39:15
Le 29 déc. 07 à 05:25, Todd O'Bryan a écrit :

>
> After wrestling with newforms to get it to do things I
thought it was
> never intended to do, I decided that either it was
really intended to
> do such things or it's just such a clean design that
getting it to do
> weird things isn't that hard.
>
> That said, I spent a lot of time fiddling around to
find the right
> incantations, so I added a section to the Cookbook[1]
called
> newforms[2] and included instructions for creating form
fields
> dynamically[3] and altering the default order[4]. If
anyone sees any
> obvious stupidness or this duplicates information
available elsewhere,
> please let me know.
>
> Todd
>

Todd,

Thanks for those useful tricks. Here are some typo
corrections, hope  
it helps:

> [4]: http://code.djangoproject.com/wiki/CookBookNewFo
rmsDynamicFields

[4]: http://code.djangoproject.com/wiki/CookBookNewFo
rmsFieldOrdering

In DynamicFields, you forgot to add 'self' as first argument
of  
__init__.
In the same function you use range(len(iterable)) which is
not really  
pythonic, I prefer this solution:

for i, question in enumerate(questions):
     self.fields['question_%d' % i] =  
forms.ChoiceField(label=questions, ...)

I know that it's a wiki and I can fix that myself but I
prefer that  
you validate this choice, maybe I miss something?

Regards,
David
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---


Re: newforms tricks
user name
2007-12-29 09:14:52
On Dec 29, 2007 4:39 AM, David Larlet <larletgmail.com> wrote:
>
> Thanks for those useful tricks. Here are some typo
corrections, hope
> it helps:
>
> > [4]: http://code.djangoproject.com/wiki/CookBookNewFo
rmsDynamicFields
>
> [4]: http://code.djangoproject.com/wiki/CookBookNewFo
rmsFieldOrdering
>
Whoops!

> In DynamicFields, you forgot to add 'self' as first
argument of
> __init__.
> In the same function you use range(len(iterable)) which
is not really
> pythonic, I prefer this solution:
>
> for i, question in enumerate(questions):
>      self.fields['question_%d' % i] =
> forms.ChoiceField(label=questions, ...)
>
Thanks for pointing this out. I didn't even know about the
enumerate function!

Both fixed.

Todd

--~--~---------~--~----~------------~-------~--~----~
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-3]

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