Django templates stripping spaces?

后端 未结 8 445
心在旅途
心在旅途 2021-01-08 00:06

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 cr

8条回答
  •  走了就别回头了
    2021-01-08 00:35

    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.

提交回复
热议问题