As part of my models, I have an IntergerField "choices".
These choices have been labelled. (Label 1 = Straw, Label 2 = Yellow...)
However, the HTML renders the integer rather than the actual labels.
What do I need to do return the labels and not the field's interger?
Is it somethng I need to do in views? Or do I address it directly in the html file?
Code below:
Models
CHOICE1=(
('',''),
(1,'Straw'),
(2,'Yellow'),
)
class Model1(models.Model):
user = models.ForeignKey(User,blank=True,on_delete=models.CASCADE)
Choice_A = models.IntegerField(choices=Choice1,default=0)
Views
def account(request):
review_list = Model1.objects.all
return render(request,"main/account.html", {'review_list':review_list})
HTML
<h6>Champagnes</h6>
{% for Model1 in review_list%}
<table >
{% if Model1.user == user%}
<tr>
<th>Text</th><th>{{Model1.Choice_A }}</th>
</tr>
{%endif%}
</table>
{% endfor %}
CodePudding user response:
You need to use the following in your template:
{{ Model1.get_Choice_A_display }}
It will render the string instead of the integer
CodePudding user response:
You do this with the .get_fieldname_display(…)
method [Django-doc], so here get_Choice_A_display
:
<th>Text</th><th>{{Model1.get_Choice_A_display }}</th>
Note: normally the name of the fields in a Django model are written in snake_case, not PascalCase, so it should be:
choice_a
instead of.Choice_A