title: Determine empty template variable in Django
body: <p>I'm not able to determine whether a variable is empty when used in the template.
  I've iterated through the whole collection and in each I'm looking for a variable <code>narrative_text</code>.</p>
  
  <p>I tested the empty variable by </p>
  
  <pre><code>{% ifnotequal narratives.narrative_text '' %}
  </code></pre>
  
  <p>I notice the control enters this block, but prints nothing/blank when the</p>
  
  <pre><code>{{  narratives.narrative_text }} 
  </code></pre>
  
  <p>is encountered.</p>
  
  <p>So, how do I precisely check if the variable is empty?</p>
  
  <p>I read <a href="https://docs.djangoproject.com/en/dev/ref/templates/api/#invalid-template-variables" rel="noreferrer">the docs</a> and found out that invalid/empty template variables are replaced by <code>''</code>.</p>
  
  <p>The doc says that</p>
  
  <blockquote>
    <p>the template system inserts the value of the <code>TEMPLATE_STRING_IF_INVALID</code> setting.</p>
  </blockquote>
  
  <p>Do we have to explicitly enter that into the <code>settings.py</code>? I tried doing so but still I haven't been able to make it work.</p>
  
  <pre><code>c=Context({ 
      "narratives_list":all_narratives,
      "patient_name":care_seeker_name
  })
  </code></pre>
  
  <p><code>all_narratives</code> is returned by a pymongo database call.</p>
  
  <pre><code>{%  for narratives in narratives_list  %}
  &lt;tr&gt;
  &lt;td class = "date_col"&gt;
      7 Aug, 2012
  &lt;/td&gt;
  {%  ifnotequal narratives.narrative_text '' %}
  &lt;td&gt;
  &lt;div class = "narrative"&gt;
      ( text narrative )
  &lt;b&gt; 
      {{ narratives.about }}
  &lt;/b&gt;
  &lt;br&gt;&lt;br&gt;
  {{ narratives.narrative_text }}
  &lt;/div&gt;  
  &lt;/td&gt;   
  {%  else %}
  &lt;td&gt;
  &lt;div class="scans"&gt;
  &lt;div class="gallery"&gt;
  &lt;b&gt; {{ narratives.about }}&lt;/b&gt;
  &lt;br&gt;&lt;br&gt;
  &lt;a href="https://udhc1-nodejstest.rhcloud.com/my_image/{{ narratives.file_id }}"&gt;
  &lt;img src="https://udhc1-nodejstest.rhcloud.com/my_image/{{ narratives.file_id }}" width="72" height="72" alt="" /&gt;
  &lt;/a&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/td&gt;
  {%  endifnotequal %}
  </code></pre>