【问题标题】:Django messages.success don't workDjango messages.success 不起作用
【发布时间】:2018-10-11 04:07:55
【问题描述】:

我在 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>

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

【问题讨论】:

  • 访问模板中的验证错误form.field_name.errors

标签: python html django django-forms


【解决方案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 %}

【讨论】:

    猜你喜欢
    • 2012-06-29
    • 2013-12-03
    • 2013-10-01
    • 2013-07-03
    • 2014-09-28
    • 2016-06-25
    • 2014-05-18
    • 2014-09-29
    • 2014-01-08
    相关资源
    最近更新 更多