Simple tag

from pdn import constants
@register.simple_tag
def get_constant(constant, value, constant_type="CHOICES_DICT"):
    try:
        return getattr(getattr(constants, constant), constant_type)[int(value)]
    except AttributeError:
        return "NC"

Exemple de constates:

VISIBILITIES = Choices(
    ('PRIVATE', 0, _(u'Privé')),
    ('SHARED', 5, _(u'Partagé')),
    ('PUBLIC', 10, _(u'Public')),
)

Côté template

Ici company.visibility vaut 0

  • {% get_constant "VISIBILITIES" company.visibility %} -> Privé
  • {% get_constant "VISIBILITIES" company.visibility "CHOICES_DICT" %} -> Privé
  • {% get_constant "VISIBILITIES" company.visibility "CHOICES_VARNAMEDICT" %} PRIVATE

PS: Merci à Haypo et Sp4rKy pour la simplification d'écriture