104

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?

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
Xar
  • 7,572
  • 19
  • 56
  • 80

4 Answers4

142

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.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
44

for the capitalize

{{ 'helLo WOrlD'|capitalize }}

output

Hello world

for the uppercase

{{ 'helLo WOrlD'|upper }}

output

HELLO WORLD
Jamil Noyda
  • 3,321
  • 2
  • 21
  • 26
37

For Capitalize

{{ 'helLo WOrlD'|capfirst }}

For UPPER CASE

{{ 'helLo WOrlD'|upper }}

For lower case

{{ 'helLo WOrlD'|lower }}

For title

{{ 'helLo WOrlD'|title }}

For ljust

{{ 'helLo WOrlD'|ljust }}

For rjust

{{ 'helLo WOrlD'|rjust }}

For wrap

{{ 'helLo WOrlD'|wrap }}

Hope It Helps

Subhransu Das
  • 413
  • 5
  • 9
3

And you can use: Filter like this

{% filter upper %}
    UPPERCASE
{% endfilter %}
saudi_Dev
  • 829
  • 2
  • 7
  • 11