How to show label of Django TextChoices on templates

Hello guys.
I want to show the label of my choices in a template.
This is my model.

class Order(models.Model):
    class StatusChoices(models.TextChoices):
        pending = "pending", "در حال انتظار"
        in_progress = "in progress", "در حال انجام"
        completed = "completed", "انجام شده"
        processing = "processing", "در حال پردازش"
        canceld = "canceld", "لغو شده"
        
    status = models.CharField(
        choices=StatusChoices.choices,
        default=StatusChoices.pending,
        max_length=50,
    )

When I try to access the labels on view everything is fine.

Order.StatusChoices.labels # ['در حال انتظار', 'در حال انجام', 'انجام شده', 'در حال پردازش', 'لغو شده']
Order.StatusChoices.canceld.name # 'canceld'
Order.StatusChoices.canceld.value # 'canceld'
Order.StatusChoices.canceld.label # 'لغو شده'

But when I try to show labels on templates it doesn’t work.

{{ orderobj.get_status_display }} // "canceld"
{{ orderobj.status }} // "canceld"
{{ order.status.label }} // EDIT : it dosen't work and i don't know why.
{{ ??? }} // "لغو شده"

And my question is how can I show labels of choices on a template?

Have your tried the label? {{ orderobj.status.label }}

See also Model field reference | Django documentation | Django

Yes, I’ve tried it but it didn’t work.

Hello
Try {{ orderobj.get_status_display }}

6 Likes

Thank you brother. :smiley:

1 Like

I have the same problem but none of the solutions listed worked :frowning:

StatusChoices(orderobj).label
1 Like

I’m wondering, if we were listing the orders, why the following is not working

{% for order in orders %}
    <p>Status: {{ order.get_status_display }}</p>
{% endfor %}

Having this in the views end:

def my_view(request):
    orders = Order.objects.all()
    return render(request, 'my_template.html', {'orders': orders})