On Sep 13, 11:06 pm, Doug B <[EMAIL PROTECTED]> wrote:
> Could you just use a collection of forms with different prefixes...
Hi Doug,
that's a good idea... but i'm still following the meta-programming
route... i tried with a form factory function which is conceptually
closer to what i thought but still doesn't work.
This is what i tried:
def room_form_factory(new_data):
room_type = IntegerField(widget=Select(choices=BEDROOM_TYPES))
room_price = IntegerField()
is_shared = BooleanField(required=False)
class RoomShared(Form):
pass
for k in new_data:
postfix = k[-3:]
if k.startswith('room_type'):
setattr(RoomShared, 'room_type_%s' % postfix, room_type)
elif k.startswith('room_price'):
setattr(RoomShared, 'room_price_%s' % postfix, room_price)
elif k.startswith('is_shared'):
setattr(RoomShared, 'is_shared_%s' % postfix, is_shared)
return RoomShared(new_data)
And this is my view code:
def add_share_room_details(request, object_id):
if request.method == 'POST':
new_data = request.POST.copy()
form = room_form_factory(new_data)
d = vars(form)
for kw in d:
print "%s = %s" % (kw, d[kw])
if form.is_valid():
return HttpResponse("Form is valid")
else:
return HttpResponse("Form NOT valid")
else:
pass
return render_to_response("house_share_room.html")
Any other idea?
Thanks,
Lorenzo
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---