【问题标题】:Bootstrap dismissable alerts引导可关闭警报
【发布时间】:2016-08-11 18:21:28
【问题描述】:

我的 django 项目中有一个表单,我想在用户单击提交时显示表单错误(如果有)。这是我的领域之一(在我转向其他领域之前努力做到这一点)

<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="title">Title
    </label>

    <div class="col-md-6 col-sm-6 col-xs-12">
        {{ form.title }}
    </div>
    <div class="col-md-3 .col-md-offset-3">
{#                                        <p class="text-danger">{% for error in form.title.errors %}{{ error }}{% endfor %}</p>#}
        <div class="alert alert-warning alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <p>{% for error in form.title.errros %} {{ error }} {% endfor %}</p>
        </div>
</div>

但是我有两个问题:

  1. 加载表单时会显示警报,但没有错误消息:

  1. 当我解除警报时,表单样式会变得混乱:

我似乎无法理解问题所在。

使用此代码,我能够在没有引导警报的情况下获得错误消息。但是,在更正后让用户关闭警报会很棒:

<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="title">Title
    </label>

    <div class="col-md-6 col-sm-6 col-xs-12">
    {{ form.title }}
    </div>
    <span class="help-block">
        <p class="text-danger">{% for error in form.title.errors %}{{ error }}{% endfor %}</p>
    </span>
</div>

【问题讨论】:

标签: django twitter-bootstrap django-forms


【解决方案1】:

您的包含警报的&lt;div&gt; 没有关闭&lt;/div&gt; 标记,当您关闭警报时会弄乱表单。至于表单加载时显示的警报 - 警报 div 中已经有一些内容(“关闭”按钮),因此它会显示出来。您必须有条件地渲染它(仅当有相应的错误要显示时):

    <div class="col-md-3 .col-md-offset-3">
        {% if form.title.errors %}
            <div class="alert alert-warning alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <p>{% for error in form.title.errors %} {{ error }} {% endfor %}</p>
            </div>
        {% endif %}
    </div>

【讨论】:

  • 我确实试过这个。但是不显示错误文本。它仅在我按照我在帖子的最后一个代码 sn-p 中显示的方式显示时才显示
  • 这可能是一个样式问题 - 错误被呈现为无序列表,所以我认为它们出现在警报底部边缘下方的隐藏区域中(请注意屏幕截图上的“关闭”按钮是如何剪辑,消息甚至更低)。您可以使用浏览器中的调试工具(Shift + Ctrl + C 用于 Windows / Linux 上的 Chrome / Firefox,不记得其他浏览器或 Mac 快捷方式)来确认它的存在并使用 CSS 来显示它。跨度>
  • 我确实在 div 上使用了开发人员工具和“检查元素”。它只显示生成的alert divx 按钮,但没有文本。
  • 啊,你有一个错字 - {% for error in form.title.errros %},现在在我的回答中更正它(错误ros而不是错误)
  • 天啊,这么愚蠢的错字花了我整个上午。非常感谢!
猜你喜欢
  • 2014-05-30
  • 1970-01-01
  • 2014-12-12
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 2018-06-04
  • 2020-07-28
相关资源
最近更新 更多