Django 在我的注册页面中返回错误的帮助文本

Django returning bad help text in my signup page

如您所见,帮助文本未呈现为 UL 而只是纯文本 这是我的代码 Forms.py:

class CustomUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = CustomUser
        now = datetime.datetime.now()
        fields = ('username', 'email', 'gender', 'security_question', 'answer', 'birth_date', 'resume')
        widgets={
            'birth_date' :DatePickerInput(
                options={
                    'maxDate':str(datetime.datetime.now()),
                }
            )
        }

Views.py:

class SignUp(generic.CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'users/signup.html'

signup.html:

{% extends 'base.html' %}


{% block title %}Sign Up{% endblock %}

{% block content %}
<div class="login-page">
  <h1>Sign up</h1>
    <form method="post" enctype="multipart/form-data">
    {% csrf_token %}
<!--
    {{ form.as_p }}
-->
        <div class="form">
            {% for field in form %}
                <p>
                    {{ field.label_tag }}<br>
                    {{ field }}
                    {% if field.help_text %}
                        <small style="color: grey">{{ field.help_text }}</small>
                    {% endif %}
                     {% for error in field.errors %}
                         <p style="color: red">{{ error }}</p>
                      {% endfor %}
                </p>
            {% endfor %}


            <button type="submit">Sign up</button>
        </div>
    </form>
</div>
{% endblock %}

谁能帮我解决这个问题?我正在使用 Django2.0.6

help_text 允许包含 HTML - 但您将其呈现为安全字符串 - 您需要使用 safe 模板过滤器以允许 HTML被渲染。

您还在 <small> 标记内呈现它,如果帮助文本包含列表,这将导致无效 HTML,就像在本例中一样。

我建议您考虑重构您的模板来处理这个问题 - 例如:

{% if field.help_text %}
    <div style="color: grey">{{ field.help_text|safe }}</div>
{% endif %}

您可能还想考虑对这些元素使用样式 类 而不是内联样式。

如果您不希望 HTML 出现在 help_text 中,那么您需要修改相关字段的 help_text