Peter Breen
Peter Breen

Reputation: 447

Django Template Tags Creating Spaces

I am working on a Django site which is live at this site. I am getting unwanted spaces in my output caused by unwanted whitespace in the HTML.

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

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.

Here is the code for the Django template:

{% extends 'police_archive/base.html' %}

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


                {% if details.incident.case_number %}
                    <a href='/police_archive/complaint/{{details.incident.case_number}}'>
                         {{details.incident.case_number}}
                    </a>
                {% 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 %}  

            </li>
        {% endfor %}


{% endblock %}

Upvotes: 2

Views: 2012

Answers (2)

rookie
rookie

Reputation: 1

It's been seven years, but for those who came here with the same issue, the post below helped me.

https://www.reddit.com/r/django/comments/1bykhnl/remove_white_space/

I basically spanned every string and used spaceless

Mine eventually looked something like this↓

{% spaceless %}
   <p>
      by 
      {% for author in article.author.all %}
         {% if forloop.last and not forloop.first%}
            <span> and </span>
         {% elif not forloop.first %}
            <span>, </span>
         {% endif%}
            <a href="#">{{author}}</a>
         {% empty %}
            <p>Unknown</p>
      {% endfor %}
   </p>
{% endspaceless%}

→by author1, author2, author3 and author4

I hope it's useful for you;)

Upvotes: 0

Will Howell
Will Howell

Reputation: 3715

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

I can't seem to find a good solution for Django's standard templating system, but it does look like Jinja offers what you're looking for. It uses a dash to strip trailing or leading whitespace:

{% for item in seq -%}
    {{ item }}
{%- endfor %}

In order to use Jinja instead of Django's default templating system, you'll have to change your settings.py file as described by Django's docs:

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

Upvotes: 1

Related Questions