简体   繁体   English

如何避免对Django字段应用无空格?

[英]How do I avoid applying spaceless to Django fields?

I'm using {% spaceless %} in my base template which helps fix a fair bit of spacing issues while maintaining readability of the template code itself, further compounded because I'm using HamlPy. 我在基本模板中使用了{% spaceless %} ,这有助于解决一些间距问题,同时保持模板代码本身的可读性,并且由于使用HamlPy而更加复杂。 However, I have some models with fields containing HTML, which are also 'despaced', leading to some incorrect rendering, eg 但是,我有一些带有包含HTML的字段的模型,这些字段也被“分隔”,导致一些不正确的渲染,例如

... <em>Some Journal</em> <strong>Issue</strong>, 1695 (2013)

where the Journal and Issue number get smashed together. 日记和发行号被粉碎的地方。 How can I avoid spaceless being applied to the fields themselves while maintaining it elsewhere? 如何在将其保留在其他位置时避免将无空间应用于字段本身?

A dumb hack of trying to prepend the field with {% endspaceless %} and after with {% spaceless %} just throws errors. 尝试在字段前加上{% endspaceless %}并在之后加上{% spaceless %} {% endspaceless %}只会引发错误。

{% spaceless %} tag removes only spaces between HTML elements, and doesn't remove any spaces between text and elements nor spaces inside text. {% spaceless %}标签仅删除HTML元素之间的空格,并且删除文本和元素之间的任何空格, 也不删除文本内部的空格。 https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#spaceless https://docs.djangoproject.com/zh-CN/1.6/ref/templates/builtins/#spaceless

Knowing that, a simple solution might be to move the space inside one of the HTML elements: 知道了,一个简单的解决方案可能是将空间移动到HTML元素之一内:

<em>Some Journal </em><strong>Issue</strong>

Another option would be to use CSS to add the space: 另一种选择是使用CSS添加空间:

.some_context em {
    margin-right: 0.5em;
}

or 要么

.some_context em:after {
    content: " ";
}

Third option might be to use &nbsp; 第三种选择可能是使用&nbsp; :

<em>Some Journal</em>&nbsp;<strong>Issue</strong>

If "non-breaking" has some unwanted effects, try &#32; 如果“不间断”有一些不良影响,请尝试&#32; instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM