我在我的 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'