简体   繁体   English

渲染textfield和charfield扼杀额外的空格(Django / Python)

[英]rendering of textfield and charfield chomps out extra whitespace (Django/Python)

I've noticed that my template is rendering my model.CharField and model.TextField without any excess whitespace. 我注意到我的模板正在渲染我的model.CharField和model.TextField而没有任何多余的空格。

For example, if I enter data such as... 例如,如果我输入数据,如...

This     is a           test


to see what  happens.

The rendered object field will appear as... 渲染的对象字段将显示为......

This is a test to see what happens.

Is this an intentional feature of Django or have I missed some filter or parameter somewhere? 这是Django的故意特征还是我错过了某些过滤器或参数?

I've checked the field itself with some debug code (print object.field) and it does contains the extra whitespace, so the problem is in the rendering side. 我用一些调试代码(print object.field)检查了字段本身,它确实包含额外的空格,所以问题出在渲染方面。

How can I allow the user to enter paragraphs of data in TextFields? 如何允许用户在TextFields中输入数据段落? How can I preserve the whitespace that the user may have entered? 如何保留用户可能输入的空白​​?

As you can see even in StackOverflow your spaces do not display, this is from the source of your question: 正如您在StackOverflow中看到的那样,您的空间不会显示,这来自您的问题的来源:

This     is a           test


to see what  happens.

Will save in the database as: 将保存在数据库中:

This     is a           test\n\n\nto see what  happens.

You have to problems when rendering as html: 渲染为html时遇到问题:

  1. Extra spaces between words are stripped on display by the browser, unless it is between <pre></pre> tags 除非在<pre></pre>标记之间,否则浏览器会显示单词之间的额外空格

  2. Linebreaks will be rendered as plain text linebreaks, which do not display in the browser unless between <pre></pre> tags. 换行符将呈现为纯文本换行符,除非在<pre></pre>标记之间,否则不会在浏览器中显示。

For spaces, you can use such a template filter to replace them with their html entity equivalent: &nbsp; 对于空格,您可以使用此类模板过滤器将其替换为与其等效的html实体: &nbsp; .

To convert database linebreaks in HTML linebreaks, use linebreaksbr built-in filters. 要在HTML换行符中转换数据库换行符,请使用linebreaksbr内置过滤器。 For example, if {{ foo }} is: test\\nbar , then {{ foo|linebreaksbr }} will render: test<br />bar 例如,如果{{ foo }}是: test\\nbar ,那么{{ foo|linebreaksbr }}将呈现: test<br />bar

  1. Create a "templatetags" folder in some of your apps with an __init__.py file in it. 在某些应用中创建一个“templatetags”文件夹 ,其中包含__init__.py文件。

  2. Save the snippet for example in someapp/templatetags/replace_tag.py 例如,在someapp / templatetags / replace_tag.py中保存代码段

  3. Load the template filter in the template as such {% load replace_tag %} 在模板加载模板过滤器 {% load replace_tag %}

  4. Combine replace and linebreaksbr as such: {{ foo|linebreaksbr|replace:" ","&nbsp;" }} 将replace和linebreaksbr结合起来{{ foo|linebreaksbr|replace:" ","&nbsp;" }} {{ foo|linebreaksbr|replace:" ","&nbsp;" }}

You can also make your own template filter that will process the text into the HTML you need. 您还可以创建自己的模板过滤器,将文本处理为您需要的HTML。 In any case, refer to the custom template filter documentation for complete information. 在任何情况下,请参阅自定义模板筛选器文档以获取完整信息。

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

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