Django print models constant array in template


Django print models constant array in template



I have a constant array in my model:


DELIVERY_TYPES = (
('self', u'one'),
('paid', u'two'),
('free', u'3')
)



in my django template I'am trying to render it:


<span style="font-size: 20px;">{{ DELIVERY_TYPES[shop.delivery_type] }}</span>



I get an error, how to print these values right?





Can you post your model where you have defined DELIVERY_TYPES and views from where you have sent the Model object to template?
– Sijan Bhandari
Jul 2 at 10:05


DELIVERY_TYPES




2 Answers
2



You can use:


{{ shop.get_delivery_type_display }}



See the docs on get_FOO_display for more info.


get_FOO_display



You could use instance of the model to access its class props:


instance


<span style="font-size: 20px;">{{ shop.DELIVERY_TYPES[shop.delivery_type] }}</span>





You can't use in Django template language.
– Alasdair
Jul 2 at 10:08






my bad, didn't notice that :D. @Alasdair's solution better btw
– Đào Minh Hạt
Jul 2 at 10:11







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to make file upload 'Required' in Contact Form 7?

Rothschild family

amazon EC2 - How to make wp-config.php to writable?