title: Displaying jinja2 form fields based on attribute value
body: <p>I'm working on a flask app and using flask-wtf to help manage my forms. </p>
  
  <p>There are 2 ways to register on my site -  Either with or without an email token.</p>
  
  <p>If the email has been confirmed the user gets an email containing:</p>
  
  <pre><code>http://127.0.0.1:5000/register/ImNsdWVtYXJpbmUzQG1haWxpbmF0b3IuY29tIg.Ca9oUQ.bRJmGYQ1wNqfcQFx1pYyoCEy2oM
  </code></pre>
  
  <p>Otherwise on the site itself the user could click on:</p>
  
  <pre><code> http://127.0.0.1:5000/register
  </code></pre>
  
  <p>The following code can handle both cases:</p>
  
  <pre><code>@blueprint.route("/register/", defaults={'token': ''}, methods=['GET', 'POST'])
  @blueprint.route("/register/&lt;token&gt;", methods=['GET', 'POST'])
  def register(token):
      form = RegisterForm(request.form, csrf_enabled=False)
      email = confirm_token(token)
      if form.validate_on_submit():
          new_user = User.create(username=form.username.data,
                                email=form.email.data,
                                 password=form.password.data,
                                 active=True)
  return render_extensions('public/register.html', form=form , email=email)
  </code></pre>
  
  <p>My form class looks like:</p>
  
  <pre><code>class RegisterForm(Form):
      username = StringField('Username', validators=[DataRequired(), Length(min=3, max=25)])
      email = StringField('Email', validators=[DataRequired(), Email(), Length(min=6, max=40)])
      password = PasswordField('Password', validators=[DataRequired(), Length(min=6, max=40)])
  </code></pre>
  
  <p>I have a jinja2 form template that looks like:</p>
  
  <pre><code>{% from "macros.html" import render_field  %}
  &lt;div class="container-narrow"&gt;
    &lt;h1&gt;Register&lt;/h1&gt;
      &lt;br/&gt;
         &lt;form id="registerForm" class="form form-register" method="POST" action="" role="form"&gt;
          {{ form.hidden_tag() }}
          {% for field in form %}
              {% if field.label!='Email' or email=='False' %}
                  {{ render_field(field) }}
              {% endif %}
          {% endfor %}
          &lt;p&gt;&lt;input class="btn btn-default btn-submit" type="submit" value="Register"&gt;&lt;/p&gt;
  &lt;/div&gt;
  {% endblock %}
  </code></pre>
  
  <p>and a jinja2 macro that looks like:</p>
  
  <pre><code>{% macro render_field(field)%}
  
  &lt;div class="form-group"&gt;
  {#                {{field.label}}#}
                  {{field(placeholder=field.label.text, class_="form-control")}}
  &lt;/div&gt;
  
  {% endmacro %}
  </code></pre>
  
  <p>Right now this code works to display all the fields in the registerForm class. I'd like to modify/filter it so that if a token is used, I would like to display and validate only 2 form fields (username, password) . If no token - all 3 . How can this be done?</p>
  
  <p>edit: switching </p>
  
  <pre><code>{% if field.label!='Email' or email=='False' %}
  </code></pre>
  
  <p>to</p>
  
  <pre><code>{% if field.label.text!='Email' or email=='False' %}
  </code></pre>
  
  <p>got it working</p>