Accessing the Input Type from forms in Django
我正在尝试访问要在 HTML 中使用的输入类型,但它似乎显示为空白。
根据:
Django:如何访问模板中的表单字段 input_type
我应该使用:
| 1 | {{field.field.widget.input_type}} | 
但它似乎返回空白。
代码:
| 1 2 3 4 5 6 7 8 9 | {% for field in form %}
   <label class="control-label" for="id_{{ field.name }}">{{ field.label }}</label> {% endfor %} | 
非常感谢。
我认为更简单的方法是将 HTML 类添加到字段属性中。你可以这样做。
| 1 | field = forms.CharField(widget=forms.TextInput(attrs={‘class’: ‘form-control’})) | 
因此,您只需要这样做:
| 1 2 | <label class="control-label" for="id_{{ field.name }}">{{ field.label }}</label> {{ field }} | 
编辑:模型 -> 表单
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/267940.html
