2

I use django-modeltranslation app in my Django 1.11 project. I successfully install app and make settings, also registered models for translation as it was recommended in docs.

Question: Is it possible to use this app outside of admin? If it possible what I need to do?

translation.py:

class ArticleTranslationOptions(TranslationOptions):
    fields = ('title', 'body',)

translator.register(Article, ArticleTranslationOptions)

settings.py:

LANGUAGE_CODE = 'ru'

LANGUAGES = (
    ('ru', _('Russian')),
    ('en', _('English')),
    ('de', _('German')),
)

MODELTRANSLATION_LANGUAGES = ('en', 'de')

forms.py:

from modeltranslation.forms import TranslationModelForm

class ArticleForm(TranslationModelForm):
    """
        Form based on "Article" model.
    """

    class Meta:
        model = Article
        fields = ('title', 'title_en', 'title_de', 'body', 'body_en', 'body_de',)

    def __init__(self, *args, **kwargs):
        super(ArticleForm, self).__init__(*args, **kwargs)
        self.fields['title'].widget.attrs = {
            'class': 'form-control',
            'id': 'title',
        }
        self.fields['title_en'].widget.attrs = {
            'class': 'form-control',
            'id': 'title_en',
        }
        self.fields['title_de'].widget.attrs = {
            'class': 'form-control',
            'id': 'title_de',
        }
        self.fields['body'].widget.attrs = {
            'class': 'form-control',
            'id': 'opt_head',
        }
        self.fields['body_en'].widget.attrs = {
            'class': 'form-control',
            'id': 'body_en',
        }
        self.fields['body_de'].widget.attrs = {
            'class': 'form-control',
            'id': 'body_de',
        }

ERROR:

Traceback (most recent call last):
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
    response = get_response(request)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\Nurzhan\PycharmProjects\CA\slider\views.py", line 41, in get
    slide_create_form = SlideForm()
  File "C:\Users\Nurzhan\PycharmProjects\CA\slider\forms.py", line 29, in __init__
    'id': 'title_en',
KeyError: 'title_en'
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193
  • admin app is optional for django translation so its pretty clear that you can use it without admin. Docs specify that how to register the models and access the translated data. http://django-modeltranslation.readthedocs.io/en/latest/usage.html – Arpit Solanki Aug 31 '17 at 16:54
  • Thank you for this link! Can you give me little example how to show different language translations in one template? Also in docs I notice ModelForm part. So it mean that I can edit that translations. Right? – Nurzhan Nogerbek Aug 31 '17 at 17:33
  • you can edit I guess but you will need admin for that. for different translation you can specify languages in your settings and then create fields in your model for different languages like title will be title_en or title_de or some other language – Arpit Solanki Aug 31 '17 at 17:35
  • I am interested in write mode (Create/Edit Form). Can you check post again pls. Is this code would be enough or I need to do something else? I have next error with this code. Can you say whats wrong?! – Nurzhan Nogerbek Sep 02 '17 at 11:23
  • @ArpitSolanki When I try to use ModelForms instead of TranslationModelForm it shows in form only fields: title and body. How to show other fields in form? – Nurzhan Nogerbek Sep 03 '17 at 19:23

1 Answers1

1

I also faced the same error and came across this question. But then I found the answer.

You are extending your form with TranslationModelForm instead you have to extend it with Django's ModelForm. Because as mentioned in docs the TranslationModelForm is strips out all translation fields.

One important thing to note here is whichever translation fields you'd like to display in the form you have to add it manually in fields, for example, title_en, title_de, title_ru etc.

from django.forms import ModelForm
class ArticleForm(ModelForm):
    class Meta:
        model = Article
        fields = ('title', 'title_en', 'title_de', 'body', 'body_en', 'body_de',)