Django messages.success不起作用

时间:2018-05-01 03:42:12

标签: python html django django-forms

我在forms.py

中创建了一条消息
def clean(self):
    request = self.request
    data = self.cleaned_data
    email  = data.get("email")
    password  = data.get("password")
    qs = User.objects.filter(email=email)
    if qs.exists():
        # user email is registered, check active/
        not_active = qs.filter(is_active=False)
        if not_active.exists():
            ## not active, check email activation
            link = reverse("account:resend-activation")
            reconfirm_msg = """Go to <a href='{resend_link}'>
            resend confirmation email</a>.
            """.format(resend_link = link)
            confirm_email = EmailActivation.objects.filter(email=email)
            is_confirmable = confirm_email.confirmable().exists()
            if is_confirmable:
                msg1 = "Please check your email to confirm your account or " + reconfirm_msg.lower()
                raise forms.ValidationError(mark_safe(msg1))
            email_confirm_exists = EmailActivation.objects.email_exists(email).exists()
            if email_confirm_exists:
                msg2 = "Email not confirmed. " + reconfirm_msg
                raise forms.ValidationError(mark_safe(msg2))
            if not is_confirmable and not email_confirm_exists:
                raise forms.ValidationError("This user is inactive.")
    user = authenticate(request, username=email, password=password)
    if user is None:
        raise forms.ValidationError("Invalid credentials")
    login(request, user)
    self.user = user
    return data

当我使用简单的HTML时,它运行良好并在我的屏幕上显示消息:

{% block content %}
<h1>Login</h1>
    <form method='POST'> {% csrf_token %}
      {{ form }}
      <button type='submit' class='btn btn-default'>Submit</button>
    </form>

{% endblock %}

但是当我使用更“复杂”的HTML时,请“请检查您的电子邮件以确认您的帐户或”只是不显示:

{% load static %}

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>Cadastro</title>

    <!-- Vendor css -->
    <link href="{% static 'lib/font-awesome/css/font-awesome.css' %}" rel="stylesheet">
    <link href="{% static 'lib/Ionicons/css/ionicons.css' %}" rel="stylesheet">

    <!-- Slim CSS -->
    <link rel="stylesheet" href="{% static 'css/slim.css' %}">

  </head>
  <body>
    {% block content %}
    <form method='POST'> {% csrf_token %}
    <div class="signin-wrapper">

      <div class="signin-box">
        <h2 class="slim-logo"><a href="index.html">Site<span>.</span></a></h2>
        <h2 class="signin-title-primary">Bem vindo de volta!</h2>
        <h3 class="signin-title-secondary">Faça login para continuar.</h3>

        <div class="form-group">
          {{ form.email }}
        </div><!-- form-group -->
        <div class="form-group mg-b-50">
          {{ form.password }}
        </div><!-- form-group -->
        <button type='submit' class="btn btn-primary btn-block btn-signin">Entrar</button>
        <p class="mg-b-0"><a href="{% url "password_change" %}">Esqueci minha senha</a></p>
        <p class="mg-b-0">Não tem conta? <a href="../cadastro">Faça um cadastro</a></p>
      </div><!-- signin-box -->

    </div><!-- signin-wrapper -->
    </form>

    {% endblock %}
  </body>
</html>

我已经调试并看到消息正在正确创建,但它没有出现。 有人知道为什么吗?

1 个答案:

答案 0 :(得分:1)

所有表格验证错误都在form.errors这是一本字典 更多信息可以在Official Django Documentation

中找到

确保form.erros为not None

{% if form.errors %}
    {% for field, errors in form.errors.items %}
        {{ errors }}
    {% endfor %}
{% endif %}

要访问单独的字段错误,您需要以下内容:form.field_name.errors

{{ form.password }}
{%  if form.password.arrors %}
    {{ form.password.errors }} <!-- {{ form.password.errors.as_text }} -->
{% endif %}