确定选项是模板中的初始值

时间:2019-01-02 16:21:01

标签: django django-templates

我正在表单中手动呈现modelchoicefield,以便可以在每个选项上插入自定义属性。我想知道我可以使用哪个模板标签来确定该选项是否为初始值,以便可以在页面加载的下拉菜单中将其设置为选定的选项。

forms.py

    self.fields['formpricing'].choices = ZERO_BLANK_CHOICE + tuple([(t.id, t) for t in FormPricingMethod.objects.filter(industryname=industry)])
    self.fields['formpricing'].queryset = FormPricingMethod.objects.filter(industryname=industry)

views.py

formpricing = userprofile.formpricing 
form3 = BasisOfPricingForm(request.POST or None, user=user, initial={'formpricing': formpricing})

模板

<div class="fieldname">Form Pricing</div>
   <div class="fieldvalue">
      <select name="formpricing" required="" id="id_formpricing">

           {% for value, object in form3.formpricing.field.choices %}

               <option typenumber="{{object.typenumber}}" value="{{value}}">
                   {{object.title}}
               </option>

           {% endfor %}    

     </select>
   </div>                       
</div>

谢谢!

1 个答案:

答案 0 :(得分:0)

知道了。

{%,如果form3.formpricing.initial.id ==值%}已选择{%endif%}