djangodjango-viewsdjango-template-filtersdjango-widgetchoicefield

Django template tag show value not key


This is probably a simple answer, but I can't seem to find it in the docs.

How do I display the value of a choice field in template tags? Using .value did not work as I thought it may.

Right now it's just displaying the Key:

user_update

when I call this template tag on my html:

{{ ticket.current_status }}

from my forms.py:

current_status = ChoiceField(
    label = "",
    choices = STATUS_CHOICES,
    widget = Select(attrs={
        'class': 'h-10 flex flex-row justify-items-left',
    })
)

and my views.py:

class StaffSupportTicketUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    ...

    def get_context_data(self, **kwargs):
        context = super(StaffSupportTicketUpdateView, self).get_context_data(**kwargs)
        context['search'] = SearchForm()
        context['ticket'] = self.get_object()
        return context

and my models.py:

class SupportTicketModel(Model):
    ...

    current_status = CharField(default='new_ticket', choices=STATUS_CHOICES, max_length=35)

    ...

finally, my STATUS_CHOICES

STATUS_CHOICES = (
('new_ticket', 'New ticket submitted'),
('user_update', 'User updated ticket'),
('need_info', "Need more information"),
('completed', 'Completed'),
)

Solution

  • Use the get_FOO_display method that is dynamically created for your field because it has choices, it will return the human readable value of the field

    {{ ticket.get_current_status_display }}