【问题标题】:Why do my if condition is not executing as expected in Django template?为什么我的 if 条件在 Django 模板中没有按预期执行?
【发布时间】:2022-07-06 02:52:08
【问题描述】:

我应该怎么做?

好的,所以我有一个注册表单,我想验证 django message == "password not strong enough" 是否为真,然后执行此 HTML 代码 sn-p

<div class="alert alert-danger alert-dismissible fade show" style="margin-bottom: 0; font-size: 13px; margin-top: 0;" role="alert">
<p style="margin-top: 0; margin-bottom: 0rem; text-align: center;">
    Your password is not strong enough. New passwords must:
    <li>Be at least minimum eight to maximum 16 characters long.</li>
    <li>Contain one or more numbers.</li>
    <li>With at least one small and one capital letter.</li>
    <li>Include at least one special character like: [@#(/)+]</li>
</p>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
</button>
</div>

如果上面的代码没有执行,则显示来自我的 Django 视图的消息,其编写如下

<div class="alert alert-danger alert-dismissible fade show" style="margin-bottom: 0; font-size: 13px; margin-top: 0;" role="alert">
<p style="margin-top: 0; margin-bottom: 0rem; text-align: center;" {% if message.tags %} {% endif %}>
    {{ message }}
</p>

<button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
</button>

来自views.py file的消息条件

if username_exists:
    messages.warning(request, "Username is not available")

if email_exists:
    messages.warning(request, "Email id is already registered")

if not pass_match:
    messages.warning(request, "Grr.. password does not match")

if not complex_pass(password):
    # complex_pass is a function which returns
    #  Boolean value either user entered password is complex or not

    messages.warning(request, "password not strong enough")

问题在于我的 Django template 中的 if-else 条件

条件的时间:message == "password not strong enough"True,我希望渲染不同的 HTML 代码,而不是显示 "password not strong enough",但在我的情况下,它只是渲染 django 消息而不是 HTML 代码 sn-p。

这是带有if-else条件的完整HTML代码

{% if messages %}
{% for message in messages %}
    {% if message == "password not strong enough" %}
        <div class="alert alert-danger alert-dismissible fade show" style="margin-bottom: 0; font-size: 13px; margin-top: 0;" role="alert">
            <p style="margin-top: 0; margin-bottom: 0rem; text-align: center;">
                Your password is not strong enough. New passwords must:
                <li>Be at least minimum eight to maximum 16 characters long.</li>
                <li>Contain one or more numbers.</li>
                <li>With at least one small and one capital letter.</li>
                <li>Include at least one special character like: [@#(/)+]</li>
            </p>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>

    {% else %}
        <div class="alert alert-danger alert-dismissible fade show" style="margin-bottom: 0; font-size: 13px; margin-top: 0;" role="alert">
            <p style="margin-top: 0; margin-bottom: 0rem; text-align: center;" {% if message.tags %} {% endif %}>
                {{ message }}
            </p>

            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        
    {% endif %}

{% endfor %}

{% endif %}

这是我在浏览器中看到的内容

【问题讨论】:

    标签: python-3.x django django-templates


    【解决方案1】:

    尝试改变这个:

    messages.warning(request, "password not strong enough")
    

    至于:

    messages.warning(request, '''<p style="margin-top: 0; margin-bottom: 0rem; text-align: center;">
                Your password is not strong enough. New passwords must:
                <li>Be at least minimum eight to maximum 16 characters long.</li>
                <li>Contain one or more numbers.</li>
                <li>With at least one small and one capital letter.</li>
                <li>Include at least one special character like: [@#(/)+]</li>
                </p>''', extra_tags='safe')
    

    然后将您的 {{ message }} 更改为:

    {% if 'safe' in message.tags %}{{ message|safe }}{% else %}{{ message }}{% endif %}
    

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 2019-08-14
      • 2017-07-31
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 2021-08-22
      • 2012-02-28
      相关资源
      最近更新 更多