呈现多个表单实例(Render multiple Form instances)

编程入门 行业动态 更新时间:2024-10-27 01:26:57
呈现多个表单实例(Render multiple Form instances)

我有一个简单的应用程序,用户应该在比赛结果上下注。 一场比赛包括两支球队,一个结果和一个赌注。 在Django管理员中创建与团队匹配,参与者将填写结果和赌注。

表单必须根据数据库中的匹配动态生成。

我的想法是每个匹配都有一个(Django)Form实例,并将这些实例传递给模板。

它可以正常工作,当我从django shell中执行它时,但是当我加载视图时不会呈现实例。

该表单看起来像这样:

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)

我的(初步)看法如下所示:

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) )

我最初的想法是,我可以将form_collection传递给模板和循环遍历集合,但是id不起作用:

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

(输出实际上是每个字母之间增加空格的字典键 - 我不知道为什么......)

它工作,如果我只传递一个窗体实例到模板,只运行内部循环。

建议非常感谢。

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.

最满意答案

要在django模板中迭代字典,您必须使用:

{% for key,value in dictionary.items %}{{ value }}{% endfor %}

因此

{% for key, value in forms.items %} {% for field in value %} {{ field }} {% endfor %} {% endfor %}

应该做的伎俩!

否则,你可以把你的表单放在一个列表中,如果你的主要目标是保持它们的顺序并且保持模板代码,那么这将更有意义!

To iterate over a dictionary in a django template you have to use:

{% for key,value in dictionary.items %}{{ value }}{% endfor %}

therefore

{% for key, value in forms.items %} {% for field in value %} {{ field }} {% endfor %} {% endfor %}

should do the trick!

Otherwise you could put your forms in a list, which would make more sense if your main goal is to preserve their order and have the template code as it is!

更多推荐

本文发布于:2023-08-03 05:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1384108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   表单   实例   Render   Form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!