Opened 4 years ago

Last modified 30 hours ago

#32340 assigned Cleanup/optimization

Usability issues with Django form fields expecting specific patterns

Reported by: Thibaud Colas Owned by: Ayush Khatri
Component: Documentation Version: dev
Severity: Normal Keywords: accessibility, usability, forms
Cc: Ülgen Sarıkavak Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

Related: #32338, #32339.

Django offers a lot of fields that expect specific formats:

  • Date and time fields – DateField, DatetimeField, TimeField, SplitDateTimeField, DurationField
  • More technical fields which have very specific formats: SlugField, JSONField, UUIDField, RegexField, GenericIPAddressField

The default widget markup for all of those fields never displays the expected format. For some fields this can somewhat be addressed via the field’s label or help text (for example having "JSON" in the label, or a description of a regex pattern in help text). But not for all. This is particularly problematic for date and time fields:

  • Even though end users know about date formats, just saying a field expects a "Date" is never enough to know which specific format is expected.
  • Django supports providing supported formats globally via settings, and at the field level. Ideally the widget would display the format(s) it’s accepting based on this configuration, rather than having to hard-code it in help text, which is prone to being incorrect / falling out of date / not being set.
  • The default error messages for invalid formats are vague ("Enter a valid date.", "Enter a valid date/time.", "Enter a valid duration.").

I don’t think this is something that can necessarily be solved for all cases directly in Django, but thought it would be worth raising nonetheless. As-is, having default templates for those widgets makes it easy for developers to overlook the need for this guidance. And we end up with forms that are impossible to fill.

Solutions I can think of are:

  • Update documentation to at least express the need for communicating the expected format to end users. Ideally have examples of communicating those formats for specific field types.
  • Find a way to auto-display the format alongside the help text for the field.
  • Update the error messages to actually display the expected formats, rather than a basic "Enter a valid <field type>".

I hope this makes sense, even though again this isn’t as clear-cut as the other forms issues I noticed.

Change History (10)

comment:1 by Carlton Gibson, 4 years ago

Component: FormsDocumentation
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

Hi Thibaud.

Initially let's accept this as a Documentation issue. Certainly we can do that right? 😀

Very happy to consider specific changes that you come up with here too!

Without some idea of what we might do though, it's a bit needsinfo — maybe targeted changes for single fields to begin...?
A proof-of-concept here would help push it forward.

Thanks! 👍

comment:2 by Thibaud Colas, 4 years ago

Thank you, I think I’ll start with getting more familiar with the documentation so I can figure out where best to address this. And as I do I’ll likely have to come up with code snippets that might inform further changes.

I think it would also be good to get feedback from others here – on the specific issues outlined here, and perhaps also more generally on how much it’s Django’s role to make sure it’s not too easy to misuse.

comment:3 by Thibaud Colas, 4 years ago

I have discussed this again with a coworker, and we think that as an immediate action it would be best to have "accessibility considerations" as a standalone page in the forms / fields documentation, or cover it separately for each field, as preferred.

For the issues listed here, this would allow us to:

  • Recommend using help_text (or template overrides?) to display the expected date format, with practical copy-paste-able examples
  • At the same time we could briefly mention why this is better than alternatives (having the format in error messages or as a placeholder attribute – both better than nothing but poor in comparison).

Additionally, in the future, we could also cover common considerations for other field types:

And broader considerations that are relevant for all field types:

  • Usage of fieldsets
  • Usage of autocomplete / autocapitalize attributes
  • Visually distinguishing between optional and required fields

---

This goes beyond what I reported here initially, and I’d still personally prefer something that could be done in Django core directly, but thought it would be worth mentioning nonetheless.

comment:4 by Carlton Gibson, 4 years ago

HI Thibauld.

I think a small section in the docs would be great. We don't want to write a whole guide, but showing some basics and (if there are canonical resources) pointing on to more would likely be in scope.

I think we can get some changes here into core too... :)

comment:5 by Surya, 4 years ago

Hi Thibauld and Tom,

I am a new contributor and after looking into issue tracker, I see this as a ticket that I would love to work on if nobody else is working on it.

In case I get to work on this, I had a couple of questions regarding what needs to be done. Carlton asked me to ask my question here, so here it goes.

  1. For the suggestion of having explicit instructions on the Django documentation about the format of the form fields, as described in the ticket, should I focus on mentioning the default formats for the following fields -

Date and time fields – DateField, DatetimeField, TimeField, SplitDateTimeField, DurationField
More technical fields which have very specific formats: SlugField, JSONField, UUIDField, RegexField, GenericIPAddressField

  1. Should the changes in the documentation extend to issues such as #32339 or #32338 on why it should be avoided?

Please forgive me if I have not been able to understand the ticket responsibility the way it is intended, and I would be very grateful if the scope of the changes required could be pointed out to me by any senior member of the community.

Thanks,
Surya

comment:6 by Thibaud Colas, 11 months ago

See PR [Accessibility guidelines for all contributors #17338 https://github.com/django/django/pull/17338] for the next step on adding accessibility docs in Django. After this, I believe [Add guidelines for accessibility in documentation #17340 https://github.com/django/django/pull/17340] will be a good step in adding accessibility considerations in documentation.

After _that_, I think we’ll be in an excellent place for someone to pick up this ticket and write the right docs. We don’t necessarily need all of this to be merged though if someone wants to already take a look. A good first step would be to identify where in Django’s documentation we could document what’s discussed in this ticket.

in reply to:  6 comment:7 by Priya, 10 months ago

Replying to Thibaud Colas:

Hi Thibaud,

After _that_, I think we’ll be in an excellent place for someone to pick up this ticket and write the right docs.

What I can understand by going through this ticket is that we need to add more explicit examples of input formats to the form fields in our Docs. To get more clarity, I would like to know if the requirement is somewhat like detailed examples stated in DateTimeField https://docs.djangoproject.com/en/5.0/ref/forms/fields/#datetimefield whereas just a link to accepted formats is given in DateField https://docs.djangoproject.com/en/5.0/ref/forms/fields/#datefield. So are we looking to provide more information here in DateField or DurationField, let's say, where link is given?

A good first step would be to identify where in Django’s documentation we could document what’s discussed in this ticket.

If my understanding of the ticket is apt, I would say adding the details within the respective fields themselves would be better as it would provide an easier understanding to the end user at a glance rather than moving to an altogether different section.

Please correct me if I am proceeding in the wrong direction.

Last edited 10 months ago by Priya (previous) (diff)

comment:8 by Ülgen Sarıkavak, 9 months ago

Cc: Ülgen Sarıkavak added

comment:9 by Ayush Khatri , 31 hours ago

"This issue can be resolved by:

Adding placeholders to widgets like DateInput and TimeInput for better usability (e.g., 'YYYY-MM-DD' for dates).

Customizing error messages in DateField and similar fields to clearly communicate the expected format.

Updating templates to display placeholders and help texts dynamically.

Enhancing documentation with examples of expected formats for various fields.

I am assigning this issue to myself and will submit a patch addressing these points soon. Please let me know if any further inputs are needed."

comment:10 by Ayush Khatri , 30 hours ago

Owner: changed from nobody to Ayush Khatri
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top