2013-01-13 62 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」是可讀值或標題。

相關問題