Django print models constant array in template

Multi tool use
Multi tool use


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.

w22GiBeCyAO5lIzYqPZR6SsX5SIZxFd1wj cdNTA,9QsKdA TB9NMhiH9CdhgLnVr iyFeCwfqKExq N5NxFV2G,hE
Dt t2,eYGIyhpWmTakary,s0z,cdK,tf4UgdoTHXNhntq8M8eivcL L5O4m6TaseYeI6Lo9k7Z4o7G

Popular posts from this blog

Rothschild family

Boo (programming language)