Reputation: 1586
I would like to give a custom id to a DIV with Crispy forms using a formset. I'm trying to use the forloop.counter inside the form layout as it says it will be injected in the template rendering but it doesn't work (it just gets rendered as such css_id='liveprice_{{forloop.counter}}'
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',
),
),
)
Upvotes: 0
Views: 521
Reputation: 1586
I found a solution. Actually, django crispy forms is smart enough to simulate a {{ forloop }} in the context in injects.
The thing is that it renders only the Nodes such as Field, HTML etc, but NOT the css_class, css_id (attributes).
Therefore the solution I used is the following:
self.helper.layout = Layout(
Div(
Div(
HTML("<div class='col-md-4' id='liveprice_{{ forloop.counter }}'></div>"),,
css_class='row',
),
),
)
I hope it helps someone..
Upvotes: 3