0

I have a couple form fields I need to format differently and am trying to use a IF statement to find the fields when rendered, but doesn't seem to be working as the documentation suggests.

I want to format country and state differently, but the code doesn't like what I'm trying.

            {% for field in customerfields %}

                {% if field.name != 'country' or 'state' %}
                    {{ field.label.upper }}
                    <div class="FormRow">
                        {{ field }}
                    </div>
                {% else %}
                    <div class="FormRow">
                        <div class="StyledSelect">
                            {{ field }}
                        </div>
                    </div>
                {% endif%}

            {% endfor %}

How do I search for more than one field? Is there a better way to do this?

M.javid
  • 6,387
  • 3
  • 41
  • 56
SteveV
  • 429
  • 3
  • 8
  • 18
  • This is not a duplicate. The provided links don't address my issue. I've tried like the examples given, but even if I try {% if field.name in ( 'country', 'state') %} I get an error "Expression expected after in" – SteveV Sep 13 '17 at 22:26
  • 1
    Use one of the fully expanded versions shown in the linked answers - they demonstrate the problem with your attempt and several ways to fix it, one of which (creating a tuple and testing with `in`) is not supported in templates. If you were trying to match any of several field names, that would be something like `{% if field.name == 'country' or field.name == 'state' %}`. Since you're trying to exclude those, either reorder your if/else or invert using the identity `~(A and B) == ~A or ~B`: {% if field.name != 'country' and field.name !='state' %}`. – Peter DeGlopper Sep 13 '17 at 22:39
  • 1
    To be clear, it's creating the tuple that's not supported in templates - if you pass one in `{% if field.name in some_names_tuple %}` should work fine. – Peter DeGlopper Sep 13 '17 at 22:42
  • Thanks @PeterDeGlopper, BOTH solutions you suggested worked. Ended up using the tuple method as it was cleaner. – SteveV Sep 13 '17 at 22:55

0 Answers0