django-modeltranslation
django-modeltranslation copied to clipboard
Form with translated fields not showed in user template
Hi,
Like @russian-master in issue #243, I need to to let users to edit some fields in several languages on frontend (not in admin). But in spite of reviewing the steps in #243 thread, I have not managed to show a form with different translated fields in my frontend.
The form is defined in forms.py and I can edit translated fields in the admin without trouble. But when I try to add manually a form with translated fields into a template, the fields do not appear. Extract of code of my template, declaring manually a form field(Notice the {{ offer_form.title_en }} for translated field title_en ):
<div class="control-group">
<label for="{{offer_form.title.html_name}}" class="control-label">
{{offer_form.title.label}}
</label>
<a class="tooltip1" title="{% trans "Write a title for the offer" %}">
<div class="controls">
{{offer_form.title_en }}
</div>
</a>
<div class="controls">
{{offer_form.title.help_text}}
</div>
</div>
And in my forms.py:
class EditOfferForm(forms.ModelForm):
place = LocationEditField(Location)
meeting_point = LocationEditField(Location, autolocate=False)
duration = forms.IntegerField(widget=forms.HiddenInput())
class Meta:
model = Offer
fields = ('title_en', 'title_es', 'resume_en', 'resume_es', 'included_price_en', 'included_price_es', 'not_included_price_en', 'not_included_price_en', 'slug_en', 'slug_es', 'place', 'meeting_point', 'duration', 'price', 'type', 'resume', 'activities')
The translated fields I want to show the forms are: "title_en" and "title_es", "resume_en" and "resume_es", "slug_en" and "slug_es", "included_price_en" and "included_price_es" and finally "not_included_price_en" and "not_included_price_es". But the form is empty after rendering the template.
My settings for modeltranslation are:
MODELTRANSLATION_DEFAULT_LANGUAGE = 'en'
MODELTRANSLATION_PREPOPULATE_LANGUAGE = 'en'
MODELTRANSLATION_FALLBACK_LANGUAGES = {'default': ('en', 'es')}
MODELTRANSLATION_AUTO_POPULATE = 'all'
What is the problem? Any ideas? Thanks in advance, Patxi.
I tried to change the declaration of EditOfferForm to inherit from TranslationModelForm
instead of ModelForm
:
from modeltranslation.forms import TranslationModelForm
class EditOfferForm(TranslationModelForm):
place = LocationEditField(Location)
meeting_point = LocationEditField(Location, autolocate=False)
duration = forms.IntegerField(widget=forms.HiddenInput())
class Meta:
model = Offer
fields = ('title_en', 'title_es', 'resume_en', 'resume_es', 'included_price_en', 'included_price_es', 'not_included_price_en', 'not_included_price_en', 'slug_en', 'slug_es', 'place', 'meeting_point', 'duration', 'price', 'type', 'resume', 'activities')
But the result is the same: The form is empty after rendering the template.
I'm experiencing the exact same problem. Does anyone know of a workaround to this problem?
So what worked for me is simply using models.ModelForm
instead of TranslationModelForm
.