Django Templates Stripping Spaces?
Solution 1:
Let me preface this by saying @DNS's answer is correct as to why the spaces are not showing.
With that in mind, this template filter will replace any spaces in the string with
Usage:
{{ "hey there world"|spacify }}
Output would be hey there world
Here is the code:
from django.template import Library
from django.template.defaultfilters import stringfilter
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
import re
register = Library()
@stringfilter
def spacify(value, autoescape=None):
if autoescape:
esc = conditional_escape
else:
esc = lambda x: x
return mark_safe(re.sub('\s', '&'+'nbsp;', esc(value)))
spacify.needs_autoescape = True
register.filter(spacify)
For notes on how template filters work and how to install them, check out the docs.
Solution 2:
Django sees the object internally as having two spaces (judging by the two underscores and two spaces in the repr
output). The fact that it only shows up with one space in the template is just how HTML works. Notice how, in the question you just asked, most of the places where you entered two spaces, only one is showing up?
From the HTML4 Spec:
In particular, user agents should collapse input white space sequences when producing output inter-word space.
As S.Lott suggested, you can verify that my guess is correct by adding debug logging, or using the Firebug plugin for Firefox or something similar, to see exactly what's getting sent to the browser. Then you'll know for sure on which end the problem lies.
If multiple spaces are really important to you, you'll need to use the
entity, though I don't know offhand how you'd get Django to encode the output of that specific object using them.
Solution 3:
There is a built-in template tag spaceless
{% spaceless %}
<p>
<a href="foo/">Foo</a>
</p>
{% endspaceless %}
Which results:
<p><a href="foo/">Foo</a></p>
Read more in django documentation.
Solution 4:
For someone having issue with django template try this :
I had a similar issue with my <optgorup>
tag, i am getting my values from django model to display under select. I simple used ''
around label option to get the complete text with space.<optgroup label='{{key}}'>
Solution 5:
if you are looking for a way to remove spaces in your html then this might be what you are looking for:
html_tag|cut:" "
cut is a builtin tag for filtering in your html. Lets say you want to use an id for each html tag to be used as a permalink to reference while you generate this dynamically then for example your solution would be:
<div id="{{category_name|cut:' ' }}">
Post a Comment for "Django Templates Stripping Spaces?"