获取Django模板中的字段类型 [英] Get the type of field in django template

查看:215
本文介绍了获取Django模板中的字段类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Django表单,我想在HTML中使用Twitter Bootstrap的CSS。
,因此我的模板如下所示:

I am using django forms and I want to use Twitter Bootstrap's css in my html. so my template looks like this:

{% for field in form %}
    <div class="form-group">
        {{ field.label_tag }}<!--Same thing as : <label for="{{field.id_for_label}}"></label> -->

        <input type="{{field.type}}" class="form-control" id="{{field.auto_id}}" placeholder="Email">
      </div>
{% endfor %}

我不知道要获取类型值。 {{field.type}}

I can't figure out out to get the type value. {{field.type}} .

有什么方法可以获取django模板中输入字段的类型吗?

Is there any way to get the type of the input field in django templates?

在此先感谢

更新:
之所以问这个问题,是因为我希望能够将bootstrap类应用于输入元素。但是在Bootstrap3中,要对输入类型使用默认的CSS,则必须向输入元素中添加表单控件类,如下所示:< input type = text class = form-control id = {{field.auto_id}}占位符=>。

如果我使用django的字段 {{field}} ,则无法添加表单控件类。
我希望这可以澄清一些事情。

If I use django's field {{field}} then I can't add the form-control class. I hope this clarifies some things.

我也看到了此应用程序 https:// github .com / dyve / django-bootstrap3 看起来像我想要的那样。令我惊讶的是django不允许访问表单类型以提供更大的灵活性。

I also saw this app https://github.com/dyve/django-bootstrap3 that looks like it does what I wanted to do. It surprises me that django doesn't allow accessing the form type to allow more flexibility.

推荐答案

根据 https:/ /stackoverflow.com/a/31538555/254553

您可以使用:

{{ [FIELD].field.widget.input_type }}

[FIELD] 是您的字段名称。

这篇关于获取Django模板中的字段类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆