# How to Iterate over the translations in HTML templates

**URL:** <https://forum.djangoproject.com/t/how-to-iterate-over-the-translations-in-html-templates/35789>\
**Category:** Templates & Frontend\
**Created:** [October 19, 2024, 11:38am UTC](https://forum.djangoproject.com/t/how-to-iterate-over-the-translations-in-html-templates/35789 "2024-10-19T11:38:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anxious01210](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anxious01210/32/2595_2.png) [@anxious01210](https://forum.djangoproject.com/u/anxious01210)\
**Post date:** [October 19, 2024, 11:38am UTC](https://forum.djangoproject.com/t/how-to-iterate-over-the-translations-in-html-templates/35789/1 "2024-10-19T11:38:36Z")

</div>

Hello,  
I have a model called Pdffile, in the model I have a choice field which needs to get translations in ckb and ar by using the rosetta I did the translations and in django admin panel when I change the language I get the equivalent translations but I do not know how to get those traslated valued in a loop in the rendered html templates:  
I am using django 4.2.14  
models.py

```auto
from django.utils.translation import gettext_lazy as _
class Pdffile(models.Model):
    filename = models.CharField(max_length=100)
    TYPE_CHOICES = (
        ('manual', _('Manual')),
        ('catalogue', _('Catalogue')),
        ('application_details', _('Application Details')),
    )
    pdf_type = models.CharField(max_length=20, choices=TYPE_CHOICES)

    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
    )
	 status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')

```

Here I needed each value not to be repeated so I did as below  
views.py

```auto
from django.shortcuts import render
from .models import Pdffile
from django.utils.translation import gettext

def DocList(request):	
    types = list(Pdffile.objects.all().values_list('pdf_type', flat=True))
    final_list = []
    for c in types:
        if c not in final_list:
            final_list.append(c)
    translated_pdf_types = [gettext("pdf_type:%s" % pdf_type) for pdf_type in final_list]
	
	context = {'translated_pdf_types': translated_pdf_types, }
    return render(request, 'library/pdffile/library_list.html', context)

```

Here in the html I need to loop over translated\_pdf\_types and get the translated strings to be shown in the user selected languag  
library\_list.html

```auto
	{% for translated_pdf_type in translated_pdf_types %}
		<li><a href="#" data-filter=".{{ translated_pdf_type }}">{{ translated_pdf_type }}</a></li>
	{% endfor %}

```

How can i achieve that is my question.  
Thanks.

---

<div class="post-metadata">

**Author:** ![claudep](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/claudep/32/5377_2.png) [@claudep](https://forum.djangoproject.com/u/claudep)\
**Post date:** [October 19, 2024, 6:12pm UTC](https://forum.djangoproject.com/t/how-to-iterate-over-the-translations-in-html-templates/35789/2 "2024-10-19T18:12:08Z")

</div>

I’m not sure I get exactly what you are trying to do, but as I understand it, I would replace the first 6 lines of your DocList function by this:

```auto
    types = list(Pdffile.objects.all().values_list('pdf_type', flat=True).distinct())
    types_dict = dict(Pdffile.TYPE_CHOICES)
    translated_pdf_types = [types_dict[val] for val in types]

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![anxious01210](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anxious01210/32/2595_2.png) [@anxious01210](https://forum.djangoproject.com/u/anxious01210)\
**Post date:** [October 20, 2024, 11:06am UTC](https://forum.djangoproject.com/t/how-to-iterate-over-the-translations-in-html-templates/35789/3 "2024-10-20T11:06:49Z")

</div>

Hi @claudep,

This is exactly what I’ve been looking for as a solution, with the code that you provided when a user visits the rendered library\_list.html and changes the language then in the below code I get the right corresponding translations:

```auto
	{% for translated_pdf_type in translated_pdf_types %}
		<li><a href="#" data-filter=".{{ translated_pdf_type }}">{{ translated_pdf_type }}</a></li>
	{% endfor %}

```

I was wondering that How can I get the first {{ translated\_pdf\_type }} in the li tag in english (the default language) in the rendered library\_list.html and the 2nd one is fine which is translated now.  
I am using django-modeltranslation and django-rosetta.  
or, is there any way so that I can get the {{ pdf.pdf\_type }} in a loop based on the visitor’s selected language in the rendered library\_list.html page, in the below code I can get the variable in english but what if I need their translations depending on the selected language by the page visitor? like:

```auto
{% for pdf in pdffiles %}
    {{ pdf.pdf_type }}
{% endfor %}

```

Thank you.
