Django Template Tags Creating Spaces
<p>I am working on a Django site which is live at <a href="http://petervkay.pythonanywhere.com/police_archive/officer/1903/" rel="nofollow">this site</a>.  I am getting unwanted spaces in my output caused by unwanted whitespace in the HTML.  </p>

<p>For instance, "01-1737 , Civilian Review Authority , INAPPROPRIATE LANGUAGE, SUSTAINED," has extra spaces before most of the commas.</p>

<p>I have found other posts with similar problems, but no solution has worked for me. I tried the {% spaceless %} tag, but that didn't work.  The only thing that did work for me was putting all of the template tags in the for loop on a single line, but I'd really like to find a more readable solution than this.</p>

<p>Here is the code for the Django template:</p>

<pre><code>{% extends 'police_archive/base.html' %}

{% block content %}
    &lt;h2&gt; {{officer.first_name}} {{officer.last_name}}, badge #{{officer.badge}} &lt;/h2&gt;
    &lt;p&gt;&lt;strong&gt;Department:&lt;/strong&gt; {{officer.department}}&lt;/p&gt;
    &lt;h2&gt;Complaints&lt;/h2&gt;
    &lt;ul&gt;
        {% for details in details_list %}
            &lt;li&gt;


                {% if details.incident.case_number %}
                    &lt;a href='/police_archive/complaint/{{details.incident.case_number}}'&gt;
                         {{details.incident.case_number}}
                    &lt;/a&gt;
                {% else %}
                 No Case Number Found
                {% endif %}


                {% if details.incident.office %}
                 , {{details.incident.get_office_display}}
                {% else %}
                , No office found
                {% endif %}

                {% if details.allegation %}
                 , {{details.allegation}}
                {% endif %}

                {% if details.finding %}
                 , {{details.finding}}
                {% endif %}

                {% if details.action %}
                 , {{details.action}}
                {% endif %}  

            &lt;/li&gt;
        {% endfor %}


{% endblock %}
</code></pre>

<p>The reason <code>{% spaceless %}</code> didn't give remove all the space for you is because it only works <strong>between</strong> HTML tags. You whitespace is showing up <strong>within</strong> the <code>&lt;li&gt;</code> tag.</p>

<p>I can't seem to find a good solution for Django's standard templating system, but it does look like <a href="http://jinja.pocoo.org/docs/dev/templates/" rel="nofollow">Jinja</a> offers what you're looking for. It uses a dash to strip trailing or leading whitespace:</p>

<pre><code>{% for item in seq -%}
    {{ item }}
{%- endfor %}
</code></pre>

<p>In order to use Jinja instead of Django's default templating system, you'll have to change your <code>settings.py</code> file as described by <a href="https://docs.djangoproject.com/en/1.10/topics/templates/#s-configuration" rel="nofollow">Django's docs</a>:</p>

<pre><code>TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2.',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            # ... some options here ...
        },
    },
]
</code></pre>

python django