On Monday, February 14, 2011 07:00:38 am ju wrote:
> 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">
 

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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to