25

How do you add a conditional CSS class based on a Python if statement in the following example to show the has-success has-feedback form element?

<div class="form-group {% if not error_username %} has-success has-feedback {% endif %}">
Robert
  • 431
  • 1
  • 6
  • 10
  • 3
    Does what you have not work? – Blender May 08 '17 at 22:23
  • 1
    It did not. I ended up running `{% if error_username %}` and then div class to run on if error, then `{% else %}` for div with no error and finally `{% endif %}` but it was a lot of code. – Robert May 09 '17 at 01:07

1 Answers1

52

Write if condition this way.

<div class="form-group {{'has-success has-feedback' if not error_username }}">
SumanKalyan
  • 1,681
  • 14
  • 24
  • 1
    Just in case: the full form allows `else` as well. The manpage: https://jinja.palletsprojects.com/en/2.11.x/templates/#if-expression – dmitry_romanov Oct 17 '21 at 08:40