Is it possible to use django-modeltranslation outside of admin?

643 views Asked by At

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'
1

There are 1 answers

0
user2429245 On

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',)