gpt4 book ai didi

django - Django过滤器返回将在模板中呈现的HTML

转载 作者:行者123 更新时间:2023-12-04 04:10:23 25 4
gpt4 key购买 nike

my_text

my_text = '''The template system works in a two-step process: compiling and rendering. A compiled template is, simply, a list of Node objects.

Thus, to define a custom template tag, you specify how the raw template tag is converted into a Node (the compilation function), and what the node’s render() method does.'''

my_filter
@register.filter(is_safe=True)
def format_description(description):
text = ''
for i in description.split('\n'):
text += ('<p class="site-description">' + i + '</p>')
return text

我的问题

我像这样在原始html中获得输出
 <p class="site-description">The template system works in a two-step process: compiling and rendering. A compiled template is, simply, a list of Node objects.</p><p class="site-description">    </p><p class="site-description">    Thus, to define a custom template tag, you specify how the raw template tag is converted into a Node (the compilation function), and what the node’s render() method does.</p>

代替

The template system works in a two-step process: compiling and rendering. A compiled template is, simply, a list of Node objects.

Thus, to define a custom template tag, you specify how the raw template tag is converted into a Node (the compilation function), and what the node’s render() method does.



这个主意

想法是获取文本并为拆分后创建的列表的每个部分创建不同的段落,以便可以将文本格式化为漂亮的
和密封性

最佳答案

要禁用自动转义,可以使用 mark_safe 方法:

from django.utils.safestring import mark_safe

@register.filter(is_safe=True)
def format_description(description):
text = ''
for i in description.split('\n'):
text += ('<p class="site-description">' + i + '</p>')
return mark_safe(text)

关于django - Django过滤器返回将在模板中呈现的HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51986727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com