I have a model class with a choice field and its possible values defined as constants, as recommended in https://docs.djangoproject.com/en/3.0/ref/models/fields/#choices
class Student(models.Model):
FRESHMAN = 'FR'
SOPHOMORE = 'SO'
JUNIOR = 'JR'
YEAR_IN_SCHOOL_CHOICES = [
(FRESHMAN, 'Freshman'),
(SOPHOMORE, 'Sophomore'),
(JUNIOR, 'Junior'),
]
year_in_school = models.CharField(
max_length=2,
choices=YEAR_IN_SCHOOL_CHOICES,
default=FRESHMAN,
)
In regular Python code (e.g. in a view), I can easily use the constants like so:
if my_student.year_in_school == Student.FRESHMAN:
# do something
My question is: can I do something like this in a template as well? Something like
{% if student.year_in_school == Student.FRESHMAN %}
Welcome
{% endif %}
... this works if I hard-code the value 'FR' in the template, but that kind of defies the purpose of constants...
(I am using Python 3.7 and Django 3.0)