Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 }}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading