As is often the case in django, they have already provided a mechanism
for what you are trying to do the hard way: Formsets.
http://docs.djangoproject.com/en/dev/topics/forms/formsets/

Cheers,
Kevin

On Apr 29, 7:18 am, MarcoS <marc.se...@gmail.com> wrote:
> Hi all,
>    I post a problem, hoping someone can help me:
>
> I want to render a dynamic form which many rows how the number of
> entries from a database table
> (something similar at the list in the "change_list.html" template in
> admin)
>
> This is the model:
>
> class Book(models.Model):
>     title = models.CharField(max_length=100)
>     quantity = models.IntegerField()
>     price = models.DecimalField(max_digits=5, decimal_places=2)
>
> So, I've create this forms.py:
>
> class choose_book_form(forms.Form):
>     def __init__(self, *args, **kwargs):
>         CHOICES = (("1", "1"), ("2", "2"), ("3", "3"))
>         super(choose_book_form, self).__init__(*args, **kwargs)
>         books = Book.objects.all()
>         for item in books:
>             checkbox = 'checkbox_%s' % item.pk
>             self.fields[checkbox] = forms.BooleanField()
>             quantity = 'quantity_%s' % item.pk
>             self.fields[faild] = forms.CharField(max_length=3,
> widget=forms.Select(choices=CHOICES))
>
> The template will have to show rows with this data
>  "checkbox" - "book title" - "select-option" - "book price"
> this is my template
>
> <form action="" method="post">
> <table>
> <tr><th>Select</th><th>Title</th><th>Quantity</th><th>Price</th>
> </tr>
> {% for item in books %}
> <tr class="{% cycle 'class1'  'class2' %}">
> <td> {{ form.checkbox_??? }} </td>
> <td> {{ item.title }} </td>
> <td> {{ form.quantity_??? }} </td>
> <td> {{ item.price }} </td>
> </tr>
> {% endfor %}
> </table>
> <input type="submit" value="Prosegui" />
> </form>
>
> The problem is that I can't figure how access the correct pk value
> for  {{ form.checkbox_ }} and {{ form.quantity_ }}
--~--~---------~--~----~------------~-------~--~----~
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