python - How to translate dynamic form choices -


i using same form , changing choices.

all of choices have translation.

do need specify in form?

forms.py

class questionform(forms.form):     selection = forms.choicefield(widget=forms.radioselect()) 

views.py

from django.utils.translation import ugettext _ form = questionform(request.post) choices = [(_(i.choice), i.choice) in question.choices.all()] form.fields['selection'].choices = choices 

template

<form method="post">{% csrf_token %}     {{ form.selection }}     <input type="submit" value="submit" class="btn"/> </form> 

i tried

{% trans form.selection %} 

but got error"

'boundfield' object has no attribute 'replace' 

(_(i.choice), i.choice) in wrong order, won't see translation. it's second item gets displayed, want have: (i.choice, _(i.choice)).


also, if want dynamic form, should creating dynamic form using form factory.

do not play form internals after you've created it.

somewhere in code:

def make_question_form_class(question):      choices = [(_(i.choice), i.choice) in question.choices.all()]       class _questionform(forms.form):          selection = forms.choicefield(choices = choices, widget=forms.radioselect())       return _questionform 

in view:

form_class = make_question_form_class(question) form = form_class(request.post) 

see this post james bennett himself more possibilities!


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -