我有一个django项目,我希望在模型本身定义的语言中显示UpdateView
ModelForm
。我想避免在URL中使用i18n模式。
要显示的对象通过表单URL中提供的UUID进行标识
http://name.tld/submit/55dbf53c-9364-4938-873f-6abad2e4ed45
我有:
型号:
class MyModel(models.Model):
unique_id = models.UUIDField(
default=uuid.uuid4,
editable=False,
unique=True,
verbose_name=_("UUID Field")
)
language = models.CharField(
default = "en",
max_length=7,
choices=settings.LANGUAGES,
verbose_name=_("Selected language")
)
的ModelForm:
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
localized_fields = '__all__'
fields = '__all__'
的UpdateView:
class MyModelUpdate(UpdateView):
model = MyModel
form_class = MyModelForm
template_name_suffix = '_form'
def get_object(self, **kwargs):
# get the uuid and find the corresponding object
obj = MyModel.objects.filter(unique_id=self.kwargs.get('uuid')).first()
if not obj or obj.is_answered:
raise Http404("Page not found")
else:
translation.activate(obj.language)
return obj
def get_success_url(self):
return reverse('submit-success')
模板:
{% load i18n %}
{% trans alert %}
<h2>{% trans "My great translated UpdateView" %}</h2>
<form action="" method="post">
{% csrf_token %}
{% form %}
</form>
但是,尽管在PO文件中提供了已翻译的字符串(使用django-admin makemessages
和django-admin compilemessages
创建,但该页面始终以英语显示(我的浏览器语言)。
我错过了什么?