#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/057.0d7ecfd13fa1d4eb77734c53b5bfa664%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to