开发者

Django dynamic form with additional meta data

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 p开发者_JAVA技巧rice 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: [                                           ] |
--------------------------------------------------------


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

Source: http://groups.google.com/group/django-users/browse_thread/thread/3629184ceb11aeef


First of all, you're thinking in PHP. Don't do that. There's no need for array-like HTML element names.

I'm not entirely sure I understand your requirements, but it sounds like a formset will do what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜