2011-08-18 62 views
0

我有形式:如何在表單中插入額外的內容?

class ItemForm(forms.ModelForm): 
    id = forms.ModelChoiceField(queryset=Item.objects.all(), widget=forms.HiddenInput()) 
    temp = forms.BooleanField(widget=forms.HiddenInput(), required=False) 
    date = forms.SplitDateTimeField(widget=forms.SplitDateTimeWidget()) 
    ... etc 

,並在模板中我有:

{% for field in itemForm %} 
     {% if field.is_hidden %} 
      {{ field }} 
     {% else %} 
      <div class="fieldWrapper"> 
       {% if field.errors %}<div class="errorbox">{% endif %} 
        <p>{{ field.label_tag }}</p> 
        <p>{{ field }}</p> 
        <p></p> 
       {% if field.errors %}<p>{{ field.errors }}</p></div>{% endif %} 
      </div> 
     {% endif %} 
    {% endfor %} 

它是不同形式的通用模板。而現在在一個形式:

class DifferentForm(forms.ModelForm): 
    id = forms.ModelChoiceField(queryset=DifferentItem.objects.all(), widget=forms.HiddenInput()) 
    option = forms.ModelChoiceField(queryset=Option.objects.all(), widget=forms.HiddenInput()) 
    (????) 
    temp = forms.BooleanField(widget=forms.HiddenInput(), required=False) 
    date = forms.SplitDateTimeField(widget=forms.SplitDateTimeWidget()) 
    ... etc 

我想提出另外一個鏈接以這樣的方式爲以這種方式來顯示:

<select ...> 
    <option>...</option> 
</select> 
(my additional link, button, text, whatever) 
<input ... 

怎麼辦呢?

回答

1

的方式,我通常這樣做是在模板:

{% for field in itemForm %} 
    {% if field.name == "option" %} 
     Custom stuff I want to go before this field 
    {% endif %} 

    <!-- Regular field stuff goes here --> 
    {% if field.is_hidden %} 
     {{ field }} 
    {% else %} 
     <div class="fieldWrapper"> 
      ... 
     </div> 
    {% endif %} 
    <!-- End Regular Field Stuff --> 

    {% if field.name == "option" %} 
     Custom stuff I want to go after the field 
    {% endif %} 
    {% if field.name == "another_field_name"%} 
     Custom stuff I want to go after the field 
    {% endif %} 
{% endfor %} 
相關問題