2013-01-13 57 views
1

我正在生成ModelForms并希望对它们在我的模板中的输出方式进行一些粒度控制。具体来说,我需要在每个选择列表中的每个单选按钮的末尾添加一些标记。尝试访问Django模板中的ModelForm字段modelChoice选择

代码:

# order-form.html 

{% load catname %} 
<form id = "order-form"> 
{% for form in forms %} 
<div id="gun-{{ forloop.counter }}"> 
    {% for field in form.fields %} 
     <div id="{{ field }}-item" class="item"> 
      <h3>{{ field|catname }}</h3> 
      {% for choice in form.field.choices %} {# <-- Help me out here #} 
       {{ choice.id }} 
       {{ choice.title }} 
      {% endfor %} 
     </div> 
    {% endfor %} 
{% endfor %} 
<button type="submit" id="standard-gun-form-submit">Continue to next step</button> 
</form> 

# views.py 
def get_form(request): 
    if request.method == 'POST': 
     if request.POST['gun_type'] == 'standard': 
      forms = [StandardGunForm(prefix=p) for p in range(0,2)] 
      return render_to_response('main/order-form.html', {'forms' : forms,}, RequestContext(request)) 

# forms.py 

class StandardGunForm(ModelForm): 
    def __init__(self, *args, **kwargs): 
     super(StandardGunForm, self).__init__(*args, **kwargs) 
     for field in self.fields: 
      if isinstance(self.fields[field], ModelChoiceField): 
       self.fields[field].empty_label = None 

    class Meta: 
     model = BaseGun 
     widgets = { 
      'FrameTuning' : RadioSelect(), 
      'FrameConnection' : RadioSelect(), 
     } 
     exclude = ('price') 

残局:标记,看起来像这样

<form id="foo"> 
<div class="category"> 
    <div class="item"> 
     <input type="radio" name="srsbzns" value="1">Option 1</input> 
     <img src="http://placekitten.com/150/150"> 
     <p>Other foo here</p> 
    </div> 
    <div class="item"> 
     <input type="radio" name="srsbzns" value="2">Option 2</input> 
     <img src="http://placekitten.com/150/150"> 
     <p>Other foo here</p> 
    </div> 
    <div class="item"> 
     <input type="radio" name="srsbzns" value="3">Option 3</input> 
     <img src="http://placekitten.com/150/150"> 
     <p>Other foo here</p> 
    </div> 
</div> 
</form> 

从贝,这将返回我想要

>>> forms = [StandardGunForm(prefix=p) for p in range(0,2)]\ 
>>> forms[0].fields['frame_tuning'].choices.queryset 

我很惊讶,这是证明很有挑战性!

奖励:我有DEBUG = True和Django调试工具栏启用。是否有可能将变量转储到浏览器,以便在深入了解时看到这些东西的样子?

谢谢!

+1

我想'{%for id,title in field.choices%}',但为什么不只是带有自定义小部件的{{field}}呢?如果您需要自定义输出,请编写自定义小部件... :-) –

+0

是的,自定义小部件可能是这样的 – starsinmypockets

回答

0
{% for choice in form.field.choices %} {# <-- Help me out here #} 
      {{ choice.id }} 
      {{ choice.title }} 
{% endfor %} 

看看你在这里做什么,你从字面上试图访问在这个循环中,这大概不存在所谓的“场”,每次场。

您需要获取要迭代的字段对象,并访问该对象的选择属性。

{% for field in form.fields %} 
    {% for choice in field.choices %} 
+0

唉,这也不起作用。在发布之前,我尝试了很多种组合。我想我会简单地写一个自定义小部件并在那里生成我的标记。 – starsinmypockets

+2

starsinmypockets,请尝试field.field.choices –

4

我不得不做类似的事情,并开始沿着这条路。我想从ModelChoiceField中创建表格行,其中每个列都有一个模型实例的不同字段(然后我允许通过JavaScript过滤表格行)。

我在Django文档中找不到它,但是对Django源代码的快速浏览显示了方法。你可以在查询集访问模型实例,像这样:

<form action="{% url 'some_view' %}" method="post"> 
    {% csrf_token %} 
    {% if form.non_field_errors %} 
     {{ form.non_field_errors }} 
    {% endif %} 

    {% for field in form %} 
     {{ field.label }} 

     {% if field.field.choices %} 
      {% for model_instance in field.field.choices.queryset %} 
      {{ model_instance.id }} 
      {% endfor %} 
     {% else %} 
      {{ field }} 
     {% endif %} 

     {% if field.errors %} 
      {{ field.errors|striptags }} 
     {% endif %} 

    {% endfor %} 

    <button type="submit">Submit</button> 
</form> 

然而,在这一点上,我们已经拆卸的部件有光泽(在我的情况下,CheckboxSelectMultiple)和使用必须重新组装HTML表单输入模板代码。我发现没有直接的方式来同时迭代ModelChoiceField来访问模型实例字段并获取选项的HTML表单标签。

也许有一种方法,但我放弃了我的尝试,并构建了自己的HTML表单,处理视图中的所有POST数据。这样结束就容易多了。 ModelForms非常好,方便,但将它们用于他们不是为之建造的东西最终会变得更加困难。

我想我会张贴这个,以防有人因为其他原因想要做这件事。希望能帮助到你。

0

很晚了,但我现在读,这是它为我工作

{% for field in form %} 
 
    {% for x, y in field.field.choices %} 
 
    {{x}} 
 
    {{y}} 
 
    {% endfor %} 
 
{% endfor %}

其中“x”是id或代码,和“y”是可读值或标题。

相关问题