How to reference a select list in a django template?

In my django model, I have the following:

PRIORITY = ( (1, 'Low'), (2, 'Normal'), (3, 'High'), ) 

Obviously, the record associated with this stores an integer. However, in my template, I would like to show priority in a readable format. How exactly will I do it?

My template:

 {% for x in items %} {{ x }} (added on {{ x.create_date }})<br> {% endfor %} 

{{ x.id }} will be the priority identifier.

Thanks in advance.

+4
source share
2 answers

Indeed, the β€œpriority” with the name and identifier is the self-propelled object that you came up with. If you just make this Priority Model and treat it as such, everything will work as it should. This is because you are trying to avoid using a system in which you are having problems.

-one
source

Assuming you set the choices parameter correctly when defining your model, Django automatically creates helper functions to display the names for you. See the documentation for additional instance methods for more details.

If your instance of model is x , and your attribute that retains priority is priority , then in your template you should use:

 {{ x.get_priority_display }} 
+20
source

All Articles