2015-05-21 28 views
2

有没有什么办法让Django Crispy-forms以一种稍微不同的方式发布复选框的布局来容纳Bootstrap Awesome Checkbox(https://github.com/flatlogic/awesome-bootstrap-checkbox)?Django Crispy Forms和Bootstrap Awesome Checkbox

注意:这不能通过CSS更改完成。 INPUT标签不再是带有真棒复选框的LABEL标签的子标签......它是与LABEL标签处于同一级别的兄弟。 脆皮形式呈现这样的:

<div class="checkbox"> 
    <label> 
    <input type="checkbox"> Check me out 
    </label> 
</div> 

但真棒-复选框需要呈现这样的:

<div class="checkbox"> 
    <input type="checkbox" id="checkbox1"> 
    <label for="checkbox1"> 
     Check me out 
    </label> 
</div> 

回答

3

有很多方法来实现这一点:

  1. 您可以简单地添加CSS到您的模型字段(如https://stackoverflow.com/a/18029091/3033586
  2. css_class kwarg(看这里http://django-crispy-forms.readthedocs.org/en/d-0/layouts.html?highlight=css_class

字段( 'your_boolean_field',css_class = '复选框基色'),

  • 您也可以总是覆盖模板。为field.html创建您自己的模板。
  • 场( 'your_boolean_field', 模板= 'SOME_PATH/boolean_field.html'),

    其中 'boolean_field.html' 是你的

    python2的版本。 7/site-packages/crispy_forms/templates/bootstrap3/field.html

    对于考试PLE我date_field.html

    {% load crispy_forms_field %} 
    
    
    <div id="div_{{ field.auto_id }}" class="form-group{% if wrapper_class %} {{ wrapper_class }}{% endif %}{% if form_show_errors%}{% if field.errors %} has-error{% endif %}{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}"> 
    {% if field.label and form_show_labels %} 
        <label for="{{ field.id_for_label }}" class="control-label {{ label_class }}{% if field.field.required %} requiredField{% endif %}"> 
         {{ field.label|safe }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %} 
        </label> 
    {% endif %} 
    
    <div class="controls {{ field_class }} input-group date"> 
        {% crispy_field field %} 
        <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span> 
    </div> 
    {% include 'bootstrap3/layout/help_text_and_errors.html' %} 
    

    +1

    你不能不再为它的标签标记的孩子添加CSS类...输入复选框场......这是一个兄弟。你如何创建自己的复选框模板,脆弱的形式将默认使用? – kenyee

    +0

    @kenyee更新回答...我不确定你是什么意思,“你不能添加一个css类...输入复选框字段不再是标签标签的孩子...它是兄弟姐妹“,你可以添加一些链接,说清楚,谢谢=) – madzohan

    +0

    感谢madzohan ...你最后关于重写模板的一点是我在文档中找不到的;-) – kenyee

    相关问题