从 ChoiceField 访问特定选择小部件/字段

发布于 2024-08-25 16:22:12 字数 519 浏览 4 评论 0原文

无论如何要访问小部件/从 ChoiceField 中呈现特定选择?

APPROVAL_CHOICES = (
    ('true', 'Approve'),
    ('false', 'Re-Submit')
)

class ProofApprovalForm(forms.Form):
    approved = forms.ChoiceField(
        choices=APPROVAL_CHOICES, 
        widget=forms.widgets.RadioSelect
    )

想要单独访问模板中的选项。

{{ form.approved.choices.true }}

将渲染真正的小部件 ...

我可以手动渲染它们,但想看看是否有处理这种情况的更干净的 pythonic/django 方式。

Anyway to access the widget/render a specific choice from a ChoiceField?

APPROVAL_CHOICES = (
    ('true', 'Approve'),
    ('false', 'Re-Submit')
)

class ProofApprovalForm(forms.Form):
    approved = forms.ChoiceField(
        choices=APPROVAL_CHOICES, 
        widget=forms.widgets.RadioSelect
    )

Would like to access the choices in the template separately.

{{ form.approved.choices.true }}

Would render the true widget <input type="radio" value="true" name="approved_0" />...

I could render them manually, but want to see if there is a cleaner pythonic/django way of handling this situation.

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

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

发布评论

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

评论(1

番薯 2024-09-01 16:22:12

仅用于使用重载的 render() 方法创建您自己的小部件或字段

Only to create your own widget or field with overloaded render() method

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