python - Django Template Tags Creating Spaces -
i working on django site live @ this site. getting unwanted spaces in output caused unwanted whitespace in html.
for instance, "01-1737 , civilian review authority , inappropriate language, sustained," has spaces before of commas.
i have found other posts similar problems, no solution has worked me. tried {% spaceless %} tag, didn't work. thing did work me putting of template tags in loop on single line, i'd find more readable solution this.
here code 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> {% 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 %}
the reason {% spaceless %}
didn't give remove space because works between html tags. whitespace showing within <li>
tag.
i can't seem find solution django's standard templating system, jinja offers you're looking for. uses dash strip trailing or leading whitespace:
{% item in seq -%} {{ item }} {%- endfor %}
in order use jinja instead of django's default templating system, you'll have change settings.py
file described django's docs:
templates = [ { 'backend': 'django.template.backends.jinja2.jinja2.', 'dirs': [], 'app_dirs': true, 'options': { # ... options here ... }, }, ]
Comments
Post a Comment