这可能是一个简单的答案,但我似乎无法在文档中找到它。
如何在模板标签中显示选择字段的值?使用value没有像我想象的那样起作用。
现在它只是显示键:
user_update
当我调用模板标签on myhtml:
{{ ticket.current_status }}
from myforms.py:
current_status = ChoiceField(
label = "",
choices = STATUS_CHOICES,
widget = Select(attrs={
'class': 'h-10 flex flex-row justify-items-left',
})
)
and myviews.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 mymodels.py:
class SupportTicketModel(Model):
...
current_status = CharField(default='new_ticket', choices=STATUS_CHOICES, max_length=35)
...
最后,mySTATUS_CHOICESSTATUS_CHOICES = (
('new_ticket', 'New ticket submitted'),
('user_update', 'User updated ticket'),
('need_info', "Need more information"),
('completed', 'Completed'),
)
使用为您的字段动态创建的get_FOO_display
方法,因为它有选项,它将返回字段的人类可读值
{{ ticket.get_current_status_display }}