Introspection tells me that django fields have a "hidden_widget" attribute. Maybe its not this attribute's given purpose, but its mere existence tempts me to use it in a template to conditionally render a hidden widget. e.g.,
Introspection告诉我django字段有一个“hidden_widget”属性。也许它不是这个属性的给定目的,但它的存在诱惑我在模板中使用它来有条件地渲染隐藏的小部件。例如。,
{% if some condition %}
{{ form.my_field }}
{% else %}
{{ form.my_field.hidden_widget }}
{% endif %}
But this doesn't work. Is it possible to use the hidden widget this way? What am I missing?
但这不起作用。是否有可能以这种方式使用隐藏的小部件?我错过了什么?
I realize I could do this by dynamically setting the widget in the init of my custom Form class based on some custom kwarg for the conditional, which I pass in to it, but the above solution (dynamically rendering the hidden widget in the template) would be way simpler, if its available.
我意识到我可以通过动态设置我的自定义Form类的init中的小部件来实现这一点,我根据条件的一些自定义kwarg传递给它,但是上面的解决方案(在模板中动态呈现隐藏的小部件)会如果它可用,那就更简单了。
1 个解决方案
#1
8
I was close, but using the wrong function. The answer, for those who are interested, is:
我很接近,但使用了错误的功能。对于那些感兴趣的人,答案是:
{{ form.my_field.as_hidden }}
#1
8
I was close, but using the wrong function. The answer, for those who are interested, is:
我很接近,但使用了错误的功能。对于那些感兴趣的人,答案是:
{{ form.my_field.as_hidden }}