Render multiple Form instances

Posted by vorpyg on Stack Overflow See other posts from Stack Overflow or by vorpyg
Published on 2010-06-01T20:06:40Z Indexed on 2010/06/07 11:32 UTC
Read the original article Hit count: 268

Filed under:
|
|

I have a simple application where users are supposed to bet on outcome of a match. A match consists of two teams, a result and a stake. Matches with teams are created in the Django admin, and participants are to fill in result and stake.

The form must be generated dynamically, based on the matches in the database.

My idea is to have one (Django) Form instance for each match and pass these instances to the template.

It works fine when I do it from django shell, but the instances aren't rendered when I load my view.

The form looks like this:

class SuggestionForm(forms.Form):
    def __init__(self, *args, **kwargs):
        try:
            match = kwargs.pop('match')
        except KeyError:
            pass
        super(SuggestionForm, self).__init__(*args, **kwargs)
        label = match
        self.fields['result'] = forms.ChoiceField(label=label, required=True, choices=CHOICES, widget=forms.RadioSelect())
        self.fields['stake'] = forms.IntegerField(label='', required=True, max_value=50, min_value=10, initial=10)

My (preliminary) view looks like this:

def suggestion_form(request):
    matches = Match.objects.all()
    form_collection = {}

    for match in matches:
        f = SuggestionForm(request.POST or None, match=match)
        form_collection['match_%s' % match.id] = f

    return render_to_response('app/suggestion_form.html', {
        'forms': form_collection,
        },
        context_instance = RequestContext(request)
        )

My initial thought was that I could pass the form_collection to the template and the loop throught the collection like this, but id does not work:

        {% for form in forms %}
            {% for field in form %}
                {{ field }}
            {% endfor %}
        {% endfor %}

(The output is actually the dict keys with added spaces in between each letter - I've no idea why…)

It works if I only pass one Form instance to the template and only runs the inner loop.

Suggestions are greatly appreciated.

© Stack Overflow or respective owner

Related posts about django

Related posts about forms