# forms.ModelForm django translation

**URL:** <https://forum.djangoproject.com/t/forms-modelform-django-translation/24003>\
**Category:** Forms & APIs\
**Created:** [September 20, 2023, 11:18am UTC](https://forum.djangoproject.com/t/forms-modelform-django-translation/24003 "2023-09-20T11:18:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![agronomofiorentini](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/agronomofiorentini/32/12446_2.png) [@agronomofiorentini](https://forum.djangoproject.com/u/agronomofiorentini)\
**Post date:** [September 20, 2023, 11:18am UTC](https://forum.djangoproject.com/t/forms-modelform-django-translation/24003/1 "2023-09-20T11:18:33Z")

</div>

Dear community,  
I am trying to use the django translation built-in functions to translate the labels of the form.

I report here the code that i am using,

models.py

```auto
from django.contrib.gis.db import models
from django.contrib.auth.models import User

from farmer.models import Farmer

# Create your models here.

class Field(models.Model):
    Farmer = models.ForeignKey(Farmer,
                               blank=True, 
                               null=True, 
                               on_delete=models.CASCADE)
    
    FieldName = models.CharField(max_length=500)
    
    User = models.ForeignKey(User, 
                             blank=True, 
                             null=True, on_delete=models.CASCADE)

    def __str__ (self):
        return f'{self.FieldName}'

```

forms.py

```auto
from django import forms
from leaflet.forms.widgets import LeafletWidget

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Field

from .models import Field
from farmer.models import Farmer

from django.utils.translation import gettext as _

class FieldForm(forms.ModelForm):
    class Meta:
        model = Field
        fields = ('Farmer', 'FieldName')
        labels = {
            "Farmer": _("Name of the farmer company"),
            "FieldName": _("Name of the field")
        }
        widgets = {
        }
    
    def __init__ (self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super(FieldForm, self). __init__ (*args, **kwargs)
        
        if user is not None:
            self.fields['Farmer'].queryset = Farmer.objects.filter(User_id=user)

```

views.py

```auto
class NewFieldFarmer(LoginRequiredMixin, CreateView):
    model = Field
    form_class = FieldForm
    template_name = 'field/field_new.html'
    success_url="/registeredfield/"

```

urls.py (app directory)

```auto
from django.urls import path

from .views import NewFieldFarmer

urlpatterns = [
    path("newfield/", NewFieldFarmer.as_view(), name="newfield")
]

```

urls.py (project directory

```auto
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("", include("field.urls")),
]

```

field\_new.html

```auto
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% load i18n %}

{% block content %}
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    {% leaflet_js plugins="forms, geocoder, measurement" %}
    {% leaflet_css plugins="forms, geocoder, measurement" %}
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <title>{% trans "New Field" %}</title>
  </head>
  <body>
    <br>
    <h3 style="text-align: center;">{% trans "New field registration" %}</h3>
    <hr>
    <div class="container">
        <div class="row align-items-start">
            <div class="col-8">
                <form method="POST">
                    {% csrf_token %}
                    {{ form|crispy }}
                    <input class= "btn btn-success" type="submit" value="Register the new field"/>
                </form>           
                <br>
            </div>
        </div>
    </div>
</div>
</body>
</html>
{% endblock %}

```

what am i doing wrong?

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [September 20, 2023, 11:31am UTC](https://forum.djangoproject.com/t/forms-modelform-django-translation/24003/2 "2023-09-20T11:31:45Z")

</div>

I assume that you’re not seeing the translated strings on the template.  
In that case i should ask, have you written the translation to the desired language using the [makemessages](https://docs.djangoproject.com/en/4.2/ref/django-admin/#django-admin-makemessages) command, and after that runned the [compilemessages](https://docs.djangoproject.com/en/4.2/ref/django-admin/#compilemessages) command?  
Also make sure you have followed the documentation on [translations](https://docs.djangoproject.com/en/4.2/topics/i18n/translation/). There’s a important note there:

> Make sure you’ve activated translation for your project (the fastest way is to check if [`MIDDLEWARE`](https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-MIDDLEWARE) includes [`django.middleware.locale.LocaleMiddleware`](https://docs.djangoproject.com/en/4.2/ref/middleware/#django.middleware.locale.LocaleMiddleware)). If you haven’t yet, see [How Django discovers language preference](https://docs.djangoproject.com/en/4.2/topics/i18n/translation/#how-django-discovers-language-preference).

---

<div class="post-metadata">

**Author:** ![agronomofiorentini](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/agronomofiorentini/32/12446_2.png) [@agronomofiorentini](https://forum.djangoproject.com/u/agronomofiorentini)\
**Post date:** [September 20, 2023, 12:50pm UTC](https://forum.djangoproject.com/t/forms-modelform-django-translation/24003/3 "2023-09-20T12:50:44Z")

</div>

Thanks to reply @leandrodesouzadev ,  
Yes i have added the middleware to the settings.py and also i have used this sequence.

1. Insert the trans tag

2. use

```auto
django-admin makemessages -l it

```

1. translate the meaning

2. use

```auto
django-admin compilemessages -l it

```

I checked if the same word is translated in another section of the site (template) and it is translated correctly.

But when I try to insert it into modelforms, it doesn’t translate for me.

Ok translation  
 ![Screenshot 2023-09-20 144942](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/d/4/d4138604675d3bc0ff8bb06729912b903117ea74.png)

No translation in the form

 ![Screenshot 2023-09-20 144952](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/5/9/5963f534b6f9317f01d937884f77fcd05f87e679.png)

---

<div class="post-metadata">

**Author:** ![agronomofiorentini](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/agronomofiorentini/32/12446_2.png) [@agronomofiorentini](https://forum.djangoproject.com/u/agronomofiorentini)\
**Post date:** [September 20, 2023, 1:46pm UTC](https://forum.djangoproject.com/t/forms-modelform-django-translation/24003/4 "2023-09-20T13:46:58Z")

</div>

I have solved the issue by replacing in the forms.py this

```auto
from django.utils.translation import gettext as _

```

with this

```auto
from django.utils.translation import gettext_lazy as _

```
