【发布时间】: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">×</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">×</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">×</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">×</span>
</button>
</div>
{% endif %}
{% endfor %}
{% endif %}
这是我在浏览器中看到的内容
【问题讨论】:
标签: python-3.x django django-templates