Search code examples
djangodjango-templatesdjango-i18n

Django UpdateView: translate form labels


I created a rather simple form using Django's UpdateView class, however, now that I want it's labels to be translated into other languages, I can't figure out how to do that.

Here is the code of the view class:

class EntityUpdate(UpdateView):
    model = Entity
    template_name = "entity/settings.html"
    fields = ["enabled"]

And in my template, all i have is:

<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="{% trans 'Save' %}" />
</form>

Where do I lookup translated strings?


Solution

  • You should mark the label as translatable in the model itself.

    class Entity(models.Model):
        enabled = models.BooleanField(verbose_name=_('enabled'))
    

    (You could do the same by overriding the definition in the form, using the label argument, but doing it in the model ensures it gets translated everywhere.)