[Django] #23927: Decouple Form field_names to its representation in HTML

7 views
Skip to first unread message

Django

unread,
Nov 27, 2014, 12:32:22 PM11/27/14
to django-...@googlegroups.com
#23927: Decouple Form field_names to its representation in HTML
--------------------------------+--------------------
Reporter: jorgecarleitao | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------
Consider a form

{{{
class SearchForm(forms.Form):
search = forms.CharField(required=False)
}}}

and a view that uses the form in a GET:

{{{
def get_names(request):

if request.method == 'GET':
form = SearchForm(request.GET)
if form.is_valid():
query_result = ...
context = {'results': query_result, 'form': form}
else:
# handle failure
else:
form = SearchForm()

return render(request, 'name.html', {'form': form})
}}}

When the form is submitted trough GET, one gets an url like
`www.example.com/persons?search=...`

However, we typically want to i18n the parameters of the URL. I don't find
an easy way to do that. The way I'm doing now is
{{{
def add_prefix(self, field_name):
# HACK: ensures 'search' appears in translations
if field_name == 'search':
return _('search')
return _(field_name) # assumes other fields exist in the
translations
}}}

The suggestion here is to add an interface on the `Form` for the user to
overwrite how the form field names are mapped to html (currently they are
mapped to `field_name`). The implementation could be something on the
lines of:

1. maps `field_name` to its translation when the form is converted to
HTML.
2. maps translation back to `field_name` when the form receives data.

The flow could be: `SearchForm()` expects `{'busca': ...}`, and
`cleaned_data` returns `{'search': ...}`.

By default this mapping is the identity (1. field_name -> field_name; 2.
field_name -> field_name), that recovers the existing situation and is
thus backward compatible.

--
Ticket URL: <https://code.djangoproject.com/ticket/23927>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 27, 2014, 2:59:17 PM11/27/14
to django-...@googlegroups.com
#23927: Decouple Form field_names to its representation in HTML
--------------------------------+--------------------------------------

Reporter: jorgecarleitao | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Is localizing form parameter names commonly done? It seems like it would
add a lot of complexity to JavaScript and CSS (e.g. it would hinder using
any name selectors). Do you know of any other web frameworks that support
this? I'd be curious if you could raise the issue on the
DevelopersMailingList to see what others think about it.

--
Ticket URL: <https://code.djangoproject.com/ticket/23927#comment:1>

Django

unread,
Nov 29, 2014, 12:06:15 PM11/29/14
to django-...@googlegroups.com
#23927: Decouple Form field_names to its representation in HTML
--------------------------------+--------------------------------------

Reporter: jorgecarleitao | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (by timgraham):

[https://groups.google.com/d/topic/django-
developers/r4PLC2apqwc/discussion django-developers thread]

--
Ticket URL: <https://code.djangoproject.com/ticket/23927#comment:2>

Django

unread,
Nov 30, 2014, 5:38:25 AM11/30/14
to django-...@googlegroups.com
#23927: Decouple Form field_names to its representation in HTML
--------------------------------+--------------------------------------
Reporter: jorgecarleitao | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: 1.7
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by aaugustin):

* status: new => closed
* resolution: => wontfix


Comment:

Closing according to the django-developers thread.

--
Ticket URL: <https://code.djangoproject.com/ticket/23927#comment:3>

Reply all
Reply to author
Forward
0 new messages