Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Model's Constants from Templates without using Context Processors

Tags:

django

I have a Model which has some constants defined, like below:

class Order(models.Model):
    WAITING = 0
    APPROVED = 1
    DISAPPROVED =2

I want to display some conditional tags in my template, for example, showing an hourglass icon if the displayed Order has the status of WAITING.

Currently I'm doing it like below, but I don't like it because that way, I need to keep track of values of every constants:

{% if order.status == 0 %}
    your order is waiting approval.
{% endif

How can I access Order.WAITING, Order.APPROVED and Order.DISAPPROVED constants from my templates? What is the correct way of what I want to achieve?

like image 331
frbry Avatar asked Mar 06 '26 08:03

frbry


1 Answers

Presumably order is an instance of Order, so it has access to the class constants already. So this would work:

if order.status == order.WAITING
like image 193
Daniel Roseman Avatar answered Mar 08 '26 10:03

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!