Official advice for printing all Django form error

2019-07-21 15:20发布

I can't figure out how to print all form error messages in a Django (1.7) template, despite following the advice in the official docs and elsewhere.

I have an account user/pass admin/admin. I've intentionally changed the minimum allowable username length in the LoginForm to 6, in an effort to get a "bad length" error, instead of a "bad user/pass combo" error (full form/view code at the bottom).

According to the official docs, the following should print all error messages:

{% if form.errors %}
   {% for error in form.errors %}
      <li><strong>{{ error|escape }}</strong></li>
   {% endfor %}
{% endif %}

But it prints only the word "username".

Changing it to this

{% for error, msg in form.errors %}
   <li><strong>{{ error|escape }}: {{ msg|escape }}</strong></li>
{% endfor %}

prints

Username
u: s

Huh?

I also tried this, but it prints out nothing:

{% for error, msg in form.non_field_errors %}
   <li><strong>{{ error|escape }}: {{ msg|escape }}</strong></li>
{% endfor %}

Same with this:

{% for error in form.non_field_errors %}
   {{error}} 
{% endfor %}

and this:

{% for error in form.non_field_errors %}
   {{error.msg}} 
{% endfor %}

The accepted answer in this question suggests this:

{% for key,value in form.errors.items %}
   {% for error in value %}
   <li>{{ error|escape }}</li>
   {% endfor %}
{% endfor %}

But, whether the escape filter is there or not, it results in

TypeError at /auth/login/ -- unsupported operand type(s) for %=: 'NoneType' and 'dict'

(According to another section in the official docs, the error object is just a dictionary.)

I also tried this, as mentioned in this question, but it gives the same error:

{% for field in form %}
   <li>{{ field.errors|escape }}</li>
{% endfor %}

What am I missing please?


Url entry:

url(r"^login/$",
    "auth_lifecycle.registration.view_login.login_maybe_remember",
    { "authentication_form": AuthenticationFormEnforceLength },
    name="login"),

view_login.py

from auth_lifecycle.models     import PASSWORD_MIN_LEN, PASSWORD_MAX_LEN
from auth_lifecycle.models     import USERNAME_MIN_LEN, USERNAME_MAX_LEN
from django                    import forms    #NOT django.contrib.auth.forms
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.views import login
from django.utils.translation  import ugettext, ugettext_lazy as _

def login_maybe_remember(request, *args, **kwargs):
    """
    Login with remember-me functionality and length checking. If the
    remember-me checkbox is checked, the session is remembered for
    SESSION_COOKIE_AGE seconds. If unchecked, the session expires at
    browser close.

    - https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-SESSION_COOKIE_AGE
    - https://docs.djangoproject.com/en/1.7/topics/http/sessions/#django.contrib.sessions.backends.base.SessionBase.set_expiry
    - https://docs.djangoproject.com/en/1.7/topics/http/sessions/#django.contrib.sessions.backends.base.SessionBase.get_expire_at_browser_close
    """
    if request.method == 'POST' and not request.POST.get('remember', None):
        #This is a login attempt and the checkbox is not checked.
        request.session.set_expiry(0)

    context = {}
    context["USERNAME_MIN_LEN"] = USERNAME_MIN_LEN
    context["USERNAME_MAX_LEN"] = USERNAME_MAX_LEN
    context["PASSWORD_MIN_LEN"] = PASSWORD_MIN_LEN
    context["PASSWORD_MAX_LEN"] = PASSWORD_MAX_LEN
    kwargs["extra_context"] = context

    return login(request, *args, **kwargs)

def get_min_max_incl_err_msg(min_int, max_int):
    """A basic error message for inclusive string length."""
    "Must be between " + str(min_int) + " and " + str(max_int) + " characters, inclusive."

view_login.py continued:

username_min_max_len_err_msg = get_min_max_incl_err_msg(USERNAME_MIN_LEN, USERNAME_MAX_LEN)
pwd_min_max_len_err_msg = get_min_max_incl_err_msg(PASSWORD_MIN_LEN, PASSWORD_MAX_LEN)

class AuthenticationFormEnforceLength(AuthenticationForm):
    """
    An `AuthenticationForm` that enforces min/max lengths.
    - https://docs.djangoproject.com/en/1.7/_modules/django/contrib/auth/forms/#AuthenticationForm

    Pass this into the login form via the `authentication_form` parameter.
    - https://docs.djangoproject.com/en/1.7/topics/auth/default/#django.contrib.auth.views.login
    Which is done in `registration/urls.py`.
    """
    username = forms.CharField(min_length=USERNAME_MIN_LEN,
                               max_length=USERNAME_MAX_LEN,
                               error_messages={
                                   'min_length': username_min_max_len_err_msg,
                                   'max_length': username_min_max_len_err_msg })
    password = forms.CharField(label=_("Password"), widget=forms.PasswordInput,
                                    min_length=PASSWORD_MIN_LEN,
                                    max_length=PASSWORD_MAX_LEN,
                                    error_messages={
                                        'min_length': pwd_min_max_len_err_msg,
                                        'max_length': pwd_min_max_len_err_msg })

Entire template:

{% load i18n %}       {# For the "trans" tag #}
{% load staticfiles %}{# References the static directory.             #}
<!DOCTYPE html>       {# Use with "{% static 'color_ajax_like.js' %}" #}
<html lang="en">
<HTML><HEAD>
     <TITLE>Login</TITLE>
     <!-- The following line makes this page pleasant to view on any device. -->
     <meta name="viewport" content="width=device-width" />
</HEAD>

<BODY>

<H1>Login</H1>

{% if form.errors %}
    {% for error in form.non_field_errors %}
        {{error.msg}}
    {% endfor %}
{% endif %}
<form method="post" id="loginForm" action="{% url 'login' %}">
{% csrf_token %}
    <table>
        <tr>
             <td>{{ form.username.label_tag }}</td>
             <td>{{ form.username }}</td>
        </tr>
        <tr>
             <td>{{ form.password.label_tag }}</td>
             <td>{{ form.password }}</td>
        </tr>
    </table>

    <label><input name="remember" type="checkbox">{% trans "Remember me" %}</label>

    <input type="submit" value="login" />
    <input type="hidden" name="next" value="{% url 'main_page' %}" />
</form>

<P>{% trans "...I forgot my password..., ...Create a new account..." %}</P>

<p><i><a href="{% url 'main_page' %}">View the main page without logging in.</a></i></p>

<script language="JavaScript">
    /*
        Before our JavaScript can be imported, the following variables need
        to be set from some Django variables. While these values could be
        hard-coded here, into the JavaScript, this allows the configuration
        to be centrally located.

        These four values come from auth_lifecycle.models and are required
        by validate_login_user_pass.js.
     */
    var minUserLen = {{ USERNAME_MIN_LEN }}; //USERNAME_MIN_LEN
    var maxUserLen = {{ USERNAME_MAX_LEN }}; //USERNAME_MAX_LEN
    var minPassLen = {{ PASSWORD_MIN_LEN }}; //PASSWORD_MIN_LEN
    var maxPassLen = {{ PASSWORD_MAX_LEN }}; //PASSWORD_MAX_LEN

    document.getElementById("id_username").focus();
</script>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js"></script>
<script type='text/javascript' src="{% static 'js/validate_login_user_pass.js' %}"></script>

</BODY></HTML>

1条回答
聊天终结者
2楼-- · 2019-07-21 16:03

form.errors is a dictionary. You can access to the key (field name, or __all__) and the value (error message) by using .items:

{% for error_field, error_message in form.errors.items %}
    {{ error_field|striptags }}: {{ error_message|striptags }}
{% endfor %}
查看更多
登录 后发表回答