title: Can you use a regular expression in a Django template conditional?
body: <p>Is it possible to determine if a template variable in a Django template satisfies a regular expression?  In the following template, I want to set the CSS class for the paragraph tag that contains the help text based on whether or not the help text for that field satisfies a regular expression.  Here is the template with some pseudocode thrown in:</p>
  <pre><code>{% for field in form.visible_fields %}
    &lt;div class=&quot;form-group&quot;&gt;
      {{ field.errors }}
      {{ field.label_tag }} {{ field }}
      {% if field.help_text %}
        {% if field.help_text|lower (BEGINS WITH SOME STRING) %}         # Pseudocode
          &lt;p class=&quot;select-help-text&quot;&gt;{{ field.help_text|safe }}&lt;/p&gt;
        {% else %}
          &lt;p class=&quot;help-text&quot;&gt;{{ field.help_text|safe }}&lt;/p&gt;
        {% endif %}
      {% endif %}
    &lt;/div&gt;
  {% endfor %}
  </code></pre>
  <p>For example, if the help_text as defined in the associated form starts with the text string &quot;Hold down Ctrl&quot;, then the CSS class should be set to <code>select-help-text</code>, otherwise it should just be set to <code>help-text</code>.</p>
  <p>I understand that Django regular expressions are based on Python regexes, but Python regex evaluations always seems to be done using the <code>re</code> module which isn't accessible in a Django template.  I also looked through the Django documentation but couldn't find a way to do this.</p>
  <p><strong>UPDATE</strong></p>
  <p>I still can't get this code to work.</p>
  <p>Melvyn, who answered below, is technically correct.  You should avoid putting conditional logic in Django templates.  To that end, I changed my template per the Django <a href="https://docs.djangoproject.com/en/3.0/topics/forms/#looping-over-the-form-s-fields" rel="nofollow noreferrer">documentation</a>:</p>
  <pre><code>{% for field in form.visible_fields %}
    &lt;div class=&quot;form-group&quot;&gt;
      {{ field.errors }}
      {{ field.label_tag }} {{ field }}
      {% if field.help_text %}
        &lt;p class=&quot;help-text&quot;&gt;{{ field.help_text|safe }}&lt;/p&gt;
      {% endif %}
    &lt;/div&gt;
  {% endfor %}
  </code></pre>
  <p>I then added an <code>__init__</code> method to my forms ModelForm class that looks at the label for the form fields that shouldn't have their help text displayed and sets <code>help_text</code> to a &quot;falsey&quot; value so the condition in the template will fail:</p>
  <pre><code>def __init__(self, *args, **kwargs):
      super(MyModelForm, self).__init__(*args, **kwargs)
      for visible in self.visible_fields():
          if visible.field.label == 'foo' or visible.field.label == 'bar' or visible.field.label == 'baz':
              visible.field.help_text = False
  </code></pre>
  <p>However, I'm still seeing the help text whether I set <code>help_text</code> to False or <code>None</code> or an empty string.  Is this some type of timing issue or have I made a mistake that I'm just not seeing?</p>