Django Templates: Form field name as variable?

2,220 views
Skip to first unread message

ju

unread,
Feb 14, 2011, 10:00:38 AM2/14/11
to Django users
How can I make this loop to print form fields where XXXXXXXX is the
value of ticket.id?

{% for ticket in zone.tickets %}
{{ ticket.id }}: {{ form.ticket_count_XXXXXXXX }}
{% endfor %}

So the output to be something like this...

1: <input ... name="ticket_count_1">
10: <input ... name="ticket_count_10">
12: <input ... name="ticket_count_12">
3: <input ... name="ticket_count_3">

Mike Ramirez

unread,
Feb 14, 2011, 1:46:12 PM2/14/11
to django...@googlegroups.com

You can create a loop that creates these fields automagically.

{% for ticket in zone.tickets %}

{{ ticket.id }} <input ... name="ticket_count_{{ ticket.id }}" />

{% endfor %}

in your view you can get it with:

ticket_name = "ticket_count_%s" %(ticket.id)

tc = request.POST[ticket_name]

# validate the data as an int, if the form class doesn't define

# ticket_count_XXXXXXX to clean it for you.

form = myForm

try:

ticket_count = int(tc)

except:

# set form errors.

form.errors = {ticket_name: "Invalid data inputed for ticket count"}

if not form.errors:

# now validate with the form data as normal, checking again for errors.

form.is_valid():

...

if your form class generates the ticket_count_XXXXXX, you can then just use the form api as normal, validate and clean then use form.cleaned_data, the form api won't really care if you generate the html or it does [1].

Mike

[1] http://docs.djangoproject.com/en/1.2/topics/forms/#customizing-the-form-template

--

patent:

A method of publicizing inventions so others can copy them.

ju

unread,
Feb 15, 2011, 4:31:07 AM2/15/11
to Django users
Thank you very much for you answer

I prefer to use validation that form class provides for me...

I tried to use <input ... name="ticket_count_{{ ticket.id }}" /> but
the other problem that I have is that I can't get errors for each
field of form :(

There is another solution that I decide to use:
http://stackoverflow.com/questions/4993625/django-templates-form-field-name-as-variable

Mike Ramirez

unread,
Feb 15, 2011, 2:24:29 PM2/15/11
to django...@googlegroups.com

On Tuesday, February 15, 2011 01:31:07 am ju wrote:

> Thank you very much for you answer

>

> I prefer to use validation that form class provides for me...

>

I do too.

> I tried to use <input ... name="ticket_count_{{ ticket.id }}" /> but

> the other problem that I have is that I can't get errors for each

> field of form :(

> .

> There is another solution that I decide to use:

> http://stackoverflow.com/questions/4993625/django-templates-form-field-name

> -as-variable

I like it too, but aren't you still stuck with validating it or are you applying a validator to each field you generate this way?

Mike

--

A physicist is an atom's way of knowing about atoms.

-- George Wald

ju

unread,
Feb 16, 2011, 4:18:37 AM2/16/11
to Django users
> I like it too, but aren't you still stuck with validating it or are you
> applying a validator to each field you generate this way?

I'm not sure that I understand the question but this is how I create
new fields

self.fields['ticket_%s' % ticket['id']] = forms.IntegerField(label =
'', initial = 0, min_value = 0, max_value = self._max_tickets_count)

and there are validation rules - integer value from 0 to
_max_tickets_count

On Feb 15, 9:24 pm, Mike Ramirez <gufym...@gmail.com> wrote:
> On Tuesday, February 15, 2011 01:31:07 am ju wrote:
>
> > Thank you very much for you answer
>
> > I prefer to use validation that form class provides for me...
>
> I do too.
>
> > I tried to use <input ... name="ticket_count_{{ ticket.id }}" /> but
> > the other problem that I have is that I can't get errors for each
> > field of form :(
> > .  
> > There is another solution that I decide to use:
> >http://stackoverflow.com/questions/4993625/django-templates-form-fiel...
Reply all
Reply to author
Forward
0 new messages