我想创建一个语言选择字段,但是我正在努力以正确的语言输出语言字段。
请在下面找到源代码
#settings.py
LANGUAGE_CODE = 'de-DE'
#model.py
from django.conf.global_settings import LANGUAGES
class Language(models.Model):
language = models.CharField(max_length=7, choices=LANGUAGES)
#formy.py
class LanguageForm(forms.ModelForm):
class Meta:
model = Language
fields = ['language']
这是我的观点:
class LanguagesView(TemplateView):
template_name = 'adminarea/settings/subpages/tab_pane_settings_languages.html'
def get_context_data(self, **kwargs):
context = super(LanguagesView, self).get_context_data(**kwargs)
context['lang_form'] = LanguageFormSet(instance=self.request.user.userstats)
然后我像这样渲染表单:
{{ lang_form }}
因此,如果我在模板中呈现表单,则将始终获得英语的语言名称。如何根据Django设置中设置的语言使用正确的翻译?
模板中的当前解决方法:
{% for x, y in mainlang_form.fields.language.choices %}
<option value="{{ x }}">{% trans y %}</option>
{% endfor %}
如何避免此变通办法?