将命名置属性添加到django-crispy-Form字段

发布于 2025-01-23 11:03:15 字数 520 浏览 2 评论 0原文

我试图将名称属性属性(x-bind:attr)添加到脆皮表单字段中,但找不到有效的解决方案。我知道使用仪表板的属性是通过使用下划线来处理的,我试图通过用双重结肠替换双重下划线来做同样的事情,但它不起作用,也没有替换。

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```

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```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫罗兰の梦幻 2025-01-30 11:03:15

您可以通过在字典中传递关键字参数来实现这一目标,并使用field使用** 拆卸操作员

Field('model', **{"x-bind:disable": "disableInput"})

这使您无法使用dashes和colons在Python变量名称中。该属性将被渲染为:

<input ... x-bind:disable="disableInput">

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">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文