I was trying to add a namespaced attribute (x-bind:attr
) to a crispy form field but I couldn't find a solution that works. I know that attributes with a dash are handled by using an underscore, I tried to do the same by replacing double underscore with the double colon but it didn't work and no replacement is made.
class ChildFormSetHelperUpdate(FormHelper):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.form_tag = False
self.include_media = False
self.layout = Layout(
Div(Field('model', x_bind__disable="disableInput"), css_class='col-md-6'),
)
self.render_required_fields = True```
CodePudding user response:
You can achieve this by passing the keyword argument in a dictionary, and expanding that into the kwargs for Field
using the **
unpacking operator:
Field('model', **{"x-bind:disable": "disableInput"})
This gets around the fact that you can't use dashes and colons in Python variable names. The attribute will be rendered as:
<input ... x-bind:disable="disableInput">