Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#31158 closed Uncategorized (wontfix)

_password_validators_help_text_html() returns HTML block level element.

Reported by: Carsten Fuchs Owned by: nobody
Component: contrib.auth Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In django.contrib.auth.password_validation, function _password_validators_help_text_html() returns a <ul> list, which is an HTML block level element. (If I understood it correctly, this was introduced in #26097.) This list is used as the help_text of a related form field.

Imho, from the view of a template author for rendering the form, getting a <ul> here is somewhat unexpected:

  • Help texts are usually brief, one line statements that are written inline in Field constructors calls; the Django documentation has numerous examples.
  • The documentation of help_text seems to expect such simple texts as well: https://docs.djangoproject.com/en/3.0/ref/forms/fields/#help-text shows the form rendered with f.as_p() at the bottom of the example.
  • In many form (template) examples that I can find, including Bootstrap's, {{ field.help_text }} is usually wrapped in <p> or <small> elements.

As having block level elements in <p> or <small> is invalid HTML, I suggest to join the strings of the password validators with a simple <br> instead. For example:

def _password_validators_help_text_html(password_validators=None):
    """
    Return an HTML string with all help texts of all configured validators,
    separated by <br> tags.
    """
    help_texts = password_validators_help_texts(password_validators)
    return mark_safe("<br>".join(escape(h) for h in help_texts))

Change History (2)

comment:1 by Mariusz Felisiak, 5 years ago

Component: Formscontrib.auth
Resolution: wontfix
Status: newclosed
Summary: _password_validators_help_text_html() returns HTML block level element_password_validators_help_text_html() returns HTML block level element.

password_validators_help_text_html() was introduced in 1daae25bdcd735151de394a5578c22257e3e5dc7, this behavior is expected and documented. If you don't like it you can always subclass forms from django.contrib.auth.forms and use you own method or password_validators_help_texts().

comment:2 by Meiyer, 4 years ago

I just came across this myself. While I accept Mariusz’s explanation (even though I do not agree, as the ul being output by the function is unexpected and breaks the typical help text output in p or small), I do think that the documentation should be updated to clarify this point and to show how to perform the customisation. In my opinion, this ticket should be reopened as requesting change of documentation.

Note: See TracTickets for help on using tickets.
Back to Top