简体   繁体   English

在Django视图中用BR标签替换纯文本中的换行符

[英]Replace line breaks in plain text with BR tag in Django views

Django provides linebreaks template tag which works similarly to a PHP function nl2br . Django提供的换行符模板标签与PHP函数nl2br类似。

It works well, however it processes the given text with every request, what is a waste of resources. 它运作良好,但是它会处理每个请求的给定文本,这是对资源的浪费。 I would rather do it once, before saving it to a database. 在将其保存到数据库之前,我宁愿执行一次。

Is there any equivalent function to use in a Django view? 在Django视图中是否可以使用任何等效功能?

linebreaks模板标记只是django.utils.html的换行符包装,因此您可以直接使用它。

Actually, if you are storing it in a database... then the resulting extra query is going to add WAY more overhead to your request response cycle than you would be saving by caching the output of some rather efficient Python text transformation. 实际上,如果将其存储在数据库中,那么所产生的额外查询将比通过缓存一些相当有效的Python文本转换的输出节省的开销增加请求响应周期的开销。

A better solution would be caching the view. 更好的解决方案是缓存视图。 Django has lots of support for very flexible caching arrangements . Django对非常灵活的缓存安排提供了很多支持。

Don't be the premature optimization guy or gal . 不要成为过早的优化专家或gal Especially such tiny optimizations. 尤其是这种微小的优化。 Most of your request response cycle is spent waiting for the database or in network latency. 您的大多数请求响应周期都花费在等待数据库或网络延迟上。

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

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