2

我在我的 Django 1.11 项目中使用django-modeltranslation应用程序。我成功安装了应用程序并进行了设置,还按照文档中的建议注册了翻译模型。

问题:是否可以在管理员之外使用此应用程序?如果可能的话我需要做什么?

翻译.py:

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

translator.register(Article, ArticleTranslationOptions)

设置.py:

LANGUAGE_CODE = 'ru'

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

MODELTRANSLATION_LANGUAGES = ('en', 'de')

表格.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',
        }

错误:

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

1 回答 1

1

我也遇到了同样的错误并遇到了这个问题。但后来我找到了答案。

您正在扩展您的表单,TranslationModelForm而您必须使用 Django 的ModelForm. 因为正如文档中提到的那样,TranslationModelForm它会删除所有翻译字段。

这里要注意的一件重要事情是,无论您想在表单中显示哪个翻译字段,都必须手动添加fields,例如title_en,、title_detitle_ru

from django.forms import ModelForm
class ArticleForm(ModelForm):
    class Meta:
        model = Article
        fields = ('title', 'title_en', 'title_de', 'body', 'body_en', 'body_de',)
于 2018-04-26T17:02:58.060 回答