Opened 9 years ago

Closed 9 years ago

#23927 closed New feature (wontfix)

Decouple Form field_names to its representation in HTML

Reported by: jorgecarleitao Owned by: nobody
Component: Forms Version: 1.7
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

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.

Change History (3)

comment:1 by Tim Graham, 9 years ago

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.

comment:3 by Aymeric Augustin, 9 years ago

Resolution: wontfix
Status: newclosed

Closing according to the django-developers thread.

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