Django dynamic form with additional meta data

57 views
Skip to first unread message

ju

unread,
Feb 11, 2011, 10:23:27 AM2/11/11
to Django users
Hello

I want to make an form for purchasing tickets. The problem is that for
every event there can be diferent types of ticket with diferent price.

For every kind of ticket I will have to create an edit box where user
can select how much tickets he wants.

Then in view class I will just display the dynamicly created form ...
the only problem that I see now is that I don't know where to save an
information for each ticket price so I can easy display it in the the
same row where the edit box is?

P.S. I'm also not sure how can I dynamicly create a form using
Django ... but this have to be easy ;)

P.S. Form have to be something like this:

--------------------------------------------------------
| Tiket Type | Price | How much? | Price |
--------------------------------------------------------
| Tiket Type Name | Price $1.00 | [ ] | Price... | [tiketkind.id
= 1]
| Tiket Type Name | Price $2.00 | [ ] | Price... | [tiketkind.id
= 12]
| Tiket Type Name | Price $3.00 | [ ] | Price... | [tiketkind.id
= 18]
| Tiket Type Name | Price $4.00 | [ ] | Price... | [tiketkind.id
= 21]
--------------------------------------------------------
| TOTAL PRICE: | ... |
--------------------------------------------------------
| Email: [ ] |
--------------------------------------------------------

Shawn Milochik

unread,
Feb 11, 2011, 10:31:50 AM2/11/11
to django...@googlegroups.com
This is pretty easy. Instead of thinking about making dynamic forms,
think about making dynamic fields.

You'll have one form. When you initialize it, you'll pass it
information about the tickets available. In the __init__ of your form
you will dynamically add field objects to the form by appending to
self.fields.
example:

self.fields['this_field_I_just_made_up'] = forms.CharField()


Notes:

1. The first thing you'll need to do in your __init__ is to pop
off your custom values.
2. The second thing you'll need to do in your __init__ is to call
the __init__ of the superclass with *args and **kwargs.

If you don't do those two things, in that order, you will get errors.

Shawn

ju

unread,
Feb 11, 2011, 11:59:22 AM2/11/11
to Django users
I tried to make this small example form

from django import forms

class PurchaseForm(forms.Form):
email = forms.EmailField()
# agree = forms.BooleanField()

def ___init__(self, *args, **kwargs):
super(PurchaseForm, self).__init__(*args, **kwargs)
self.fields['agree'] = forms.BooleanField()

but this new field agree doesn't shows if it's added after calling
super...

Am I missing something?

Shawn Milochik

unread,
Feb 11, 2011, 12:06:10 PM2/11/11
to django...@googlegroups.com
You could be missing something, but I don't have enough to go on. Did
you add the field to your template? What do your view and template
look like?

ju

unread,
Feb 11, 2011, 12:11:10 PM2/11/11
to Django users
View
=========

def detail(request, event_id):
event = get_object_or_404(Event, pk = event_id)

if request.POST :
purchaseForm = PurchaseForm(request.POST)
#...
else:
purchaseForm = PurchaseForm()

return render_to_response('events/detail.html', {'event': event,
'form' : purchaseForm})

Template:
=========

<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

=========

Do I have to add the new fields also in Template or they will be shown
automatically?

ju

unread,
Feb 11, 2011, 12:27:00 PM2/11/11
to Django users
I found my mistake i made ___init__ function instead of __init__ :)
Reply all
Reply to author
Forward
0 new messages