我在Django 1.11项目中使用django-modeltranslation app。我成功安装了应用程序并进行了设置,也注册了文档中推荐的翻译模型。
问题:是否可以在管理员之外使用此应用?如果有可能我需要做什么?
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',
}
错误:
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'
答案 0 :(得分:0)
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',)