【问题标题】:Bootstrap 3 - Form horizontal not workingBootstrap 3 - 水平表格不起作用
【发布时间】:2014-01-24 03:59:39
【问题描述】:

我正在使用DjangoCrispyForms,并且我正在将我的一个旧项目更新为BS3

我唯一不知道如何适应的是form-horizontal。我的表单过去是这样的:

现在标签始终位于输入的顶部 - 就像之前使用 form-vertical 一样。

我在 Stack 上 read 一些 posts,用谷歌搜索,但没有人为我提供 crispy 的有效答案!

最奇怪的是Bootstrap的家伙say that they did not change or remove这个类。

有什么想法可以让我的旧的、可爱的 horizo​​ntal` 表格恢复原状吗?

谢谢!

更新:

CrispyForms 生成以下内容,即使使用 bootstrap3 作为模板包:

<div class="form-group" id="div_id_title">
    <label class="control-label  requiredField" for="id_title">Titel
        <span class="asteriskField">*</span>
    </label>
    <div class="controls ">
        <input type="text" name="title" maxlength="65" id="id_title" class="textinput textInput form-control"> 
    </div>
</div>

【问题讨论】:

    标签: django twitter-bootstrap twitter-bootstrap-3 django-crispy-forms


    【解决方案1】:

    看这里:

    http://django-crispy-forms.readthedocs.org/en/latest/crispy_tag_forms.html#bootstrap3-horizontal-forms

    你应该添加

    helper.label_class = 'col-lg-x' #for example 'col-lg-2'
    helper.field_class = 'col-lg-x' #for example 'col-lg-10'
    

    BS3 有一个 12 列的网格系统,更多信息在这里 http://getbootstrap.com/css/#grid

    这使表单水平,但不幸的是标签向左对齐,我现在正在尝试解决这个问题。

    更新:如果您对字段之间的垂直间距有问题,请在每个字段的外部 div 中添加一个行 css 类,html 输出:

    <div class="form-group row" id="div_id_title">
    
        ###labels HTML###
    
    </div>
    

    可能是 bootstrap3 的 field.html 模板上的一个错误。

    【讨论】:

      【解决方案2】:

      你把form-control 放在form-group 里面了吗?

      <form class="form-horizontal" role="form">
      <div class="form-group">
          <label lass="col-sm-2 control-label">Password</label>
          <div class="col-sm-10">
            <input type="password" class="form-control" id="inputPassword" placeholder="Password">
          </div>
      </div>..
      </form>
      

      演示:http://bootply.com/104095

      【讨论】:

      • 问题似乎是 CrispyForms 没有正确设置类 - 即使使用 'bootstrap3' 作为模板包......我会用一个例子更新我的答案
      • 我遇到了完全相同的问题。标签类被设置为“control-label requiredField”
      【解决方案3】:

      我花了几个小时试图弄清楚这一点。对我来说,这行得通。您必须进入项目 settings.py 并添加以下行:

      CRISPY_TEMPLATE_PACK = 'bootstrap3'
      

      通过文档搜索后,我最终发现我必须在crispy-forms 源代码本身中执行此操作:

             if (
                  TEMPLATE_PACK == 'bootstrap3'
                  and not is_checkbox(field)
                  and not is_file(field)
              ):
                  css_class += ' form-control'
      

      【讨论】:

        猜你喜欢
        • 2017-10-26
        • 2020-10-26
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 2019-01-06
        • 2021-12-26
        • 2014-12-11
        • 2014-09-28
        相关资源
        最近更新 更多