给自定义标识符以django脆皮的形式

问题描述:

我想用一个formset给脆弱形式的DIV自定义标识。 我试图在表单布局中使用forloop.counter,因为它表示它将在模板呈现中注入,但它不起作用(它只是呈现为这样的css_id ='liveprice _ {{forloop.counter}} “给自定义标识符以django脆皮的形式

class PrinterMaterialForm(ModelForm): 
    class Meta: 
     model = PrinterMaterial 
     fields = ('material', 'hour_cost', 'gram_cost', 'colors') 

    def __init__(self, *args, **kwargs): 
     super(PrinterMaterialForm, self).__init__(*args, **kwargs) 
     self.helper = FormHelper() 
     self.helper.form_tag = False 
     self.helper.form_class = 'form-inline' 
     self.helper.field_template = 'bootstrap3/layout/inline_field.html' 
     self.helper.disable_csrf = True 
     self.helper.layout = Layout(
      Div(
       Div(
        Div(css_class='col-md-4', css_id='liveprice_{{ forloop.counter }}'), 
        css_class='row', 
       ), 
      ), 
     ) 

我找到了解决办法。 事实上,Django的酥脆形式是足够聪明的模拟{{for循环}}在喷射范围内。

的事情是,它使得只有节点,作为字段,HTML等,但不是css_class,css_id(attributes)。

因此,我使用的解决方案是以下:

self.helper.layout = Layout(
    Div(
     Div(
      HTML("<div class='col-md-4' id='liveprice_{{ forloop.counter }}'></div>"),, 
      css_class='row', 
     ), 
    ), 
) 

我希望它可以帮助别人..