16

I'm having trouble with Django templates and CharField models.

So I have a model with a CharField that creates a slug that replaces spaces with underscores. If I create an object, Somename Somesurname, this creates slug Somename_Somesurname and gets displayed as expected on the template.

However, if I create an object, Somename Somesurname (notice the second space), slug Somename__Somesurname is created, and although on the Django console I see this as <Object: Somename Somesurname>, on the template it is displayed as Somename Somesurname.

So do Django templates somehow strip spaces? Is there a filter I can use to get the name with its spaces?

8 Answers 8

36

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 &nbsp;

Usage:

{{ "hey there  world"|spacify }}

Output would be hey&nbsp;there&nbsp;&nbsp;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.

0
11

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 &nbsp; entity, though I don't know offhand how you'd get Django to encode the output of that specific object using them.

1
  • +1: What you see in the browser is not what's really there. Might want to add some hints about using logging to see what's really there.
    – S.Lott
    Apr 6, 2009 at 11:48
11

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.

2
  • woah, this is awesome for display:inline-block instead of using manual html comments in between elements that then get "cleaned up" by the IDE.
    – Mario
    Oct 13, 2015 at 16:12
  • what about tag attribute? i have long loop with may conditions in template, and it gives me tons of whitespaces
    – holms
    Mar 12, 2018 at 16:52
2

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:' ' }}">

django documentation

1

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}}'>

0

Slugify removes all leading spaces, you'll need to rewrite this as a custom template tag to get the behaviour you're after. The original filter code looks like this

def slugify(value):
   """
   Normalizes string, converts to lowercase, removes non-alpha characters,
   and converts spaces to hyphens.
   """
   import unicodedata
   value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
   value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
   return mark_safe(re.sub('[-\s]+', '-', value))

which changes "some   string" into "some-string" killing the extra whitespace. You could change it like so:

def new_slugify(value):
   """
   Normalizes string, converts to lowercase, removes non-alpha characters,
   and converts spaces to hyphens, does not remove leading whitespace.
   """
   import unicodedata
   value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
   value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
   return mark_safe(re.sub('[-\s]', '-', value))

Which will result in the following behaviour: "Some  String here" to "some--string-here"

You still might have problems as mentioned before with how html treats extra whitespace, you'd have to write another, deslugify filter.

0

This tag keeps spaces and newlines. I copied Django's own tag linebreaksbr and then added a line to replace the spaces with nbsp. It does not replace single spaces so text source is still readable. I wrote this because I couldn't get the spacify tag (other answer) to work with linebreaksbr.

from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe, SafeData
from django.utils.text import normalize_newlines
from django.utils.html import escape

@register.filter(is_safe=True, needs_autoescape=True)
@stringfilter
def keep_spacing(value, autoescape=None):
    autoescape = autoescape and not isinstance(value, SafeData)
    value = normalize_newlines(value)
    if autoescape:
        value = escape(value)
    value = mark_safe(value.replace('  ', ' &nbsp;'))             
    return mark_safe(value.replace('\n', '<br />'))
0

'\s' might be ok in the use case described above, but be careful, this replaces other whitespaces like '\t' or '\n' as well! If this is not what you want, just use " " instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.