问题
I am trying to convert to upper case a string in a Jinja template I am working on.
In the template documentation, I read:
upper(s)
Convert a value to uppercase.
So I wrote this code:
{% if student.department == "Academy" %}
Academy
{% elif upper(student.department) != "MATHS DEPARTMENT" %}
Maths department
{% endif %}
But I am getting this error:
UndefinedError: 'upper' is undefined
So, how do you convert a string to uppercase in Jinja2?
回答1:
Filters are used with the |filter syntax:
{% elif student.department|upper != "MATHS DEPARTMENT" %}
Maths department
{% endif %}
or you can use the str.upper() method:
{% elif student.department.upper() != "MATHS DEPARTMENT" %}
Maths department
{% endif %}
Jinja syntax is Python-like, not actual Python. :-)
回答2:
And you can use: Filter like this
{% filter upper %}
UPPERCASE
{% endfilter %}
来源:https://stackoverflow.com/questions/23195321/how-to-convert-string-to-uppercase-lowercase-in-jinja2