normalizing newlines in form fields

60 views
Skip to first unread message

Jakub Kleň

unread,
Nov 12, 2018, 9:58:39 AM11/12/18
to Django developers (Contributions to Django itself)
I'm thinking if it wouldn't be nice if django automatically normalized newlines of data entered by the user. The implementation would be really simple, there's already a function for it in django.utils.text.normalize_newlines, and it would go into django.forms.fields.CharField.to_python the same as split() (maybe also with an ivar for controlling the behavior, although I'm not sure if it's needed). What do you think?

Tim Graham

unread,
Nov 12, 2018, 10:34:36 AM11/12/18
to Django developers (Contributions to Django itself)
Here's a ticket that was closed as wontfix. If you want it reconsidered, you should explain why you the think the arguments there aren't valid.

Jakub Kleň

unread,
Nov 12, 2018, 10:37:13 AM11/12/18
to Django developers (Contributions to Django itself)
Reason: Having consistent data representation in the database is nice, and maybe also useful in some use-cases, e.g. using the data to train a deep learning model. This tweak seems it could only help, and shouldn't break anything. If, for some reason a developer needs to use \r\n for display, he cannot rely on the client to always send \r\n in the form data, because OSX or unix sends just \n, so it seems to me that if I would need \r\n for display, It would also be nice to have just \n in the database, and replace it using a template filter during html construction.

Jakub Kleň

unread,
Nov 12, 2018, 10:49:34 AM11/12/18
to Django developers (Contributions to Django itself)
Regarding the link you sent, I also think that the current behaviour is inconsistent. I'm now not sure if we should go with \n, or \r\n, but I think we definitely should normalize this. If you are writing to files on windows, all your browsers would send \r\n over post, so the files would be generated properly, but as soon as you deploy, and the first client enters some data from OSX for example, you have a problem which you couldn't even notice before. What I'm trying to say here is that I think it's better if it was consistent, and you could catch the issue during implementation.


On Monday, November 12, 2018 at 3:58:39 PM UTC+1, Jakub Kleň wrote:

Collin Anderson

unread,
Nov 12, 2018, 12:30:17 PM11/12/18
to django-d...@googlegroups.com
Maybe the newline character could be part of the setting, something like normalize_newlines_to='\n'. And then just do a normalize_newlines_to.join(value.splitlines()) could work.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/3c773fc0-7049-4d66-ae14-9a322e884774%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jakub Kleň

unread,
Nov 12, 2018, 12:39:04 PM11/12/18
to Django developers (Contributions to Django itself)
From what I have read so far, it would be probably better to normalize to '\r\n'.


On Monday, November 12, 2018 at 3:58:39 PM UTC+1, Jakub Kleň wrote:
Reply all
Reply to author
Forward
0 new messages