Django过滤器返回将在模板中呈现的HTML [英] Django filters to return HTML that will be rendered in the template

查看:177
本文介绍了Django过滤器返回将在模板中呈现的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

my_text



  my_text =’’’模板系统分两步工作:编译和渲染。简单来说,已编译的模板就是Node对象的列表。 

因此,要定义一个自定义模板标签,您可以指定如何将原始模板标签转换为Node(编译函数)以及该节点的render()方法的作用。'''



my_filter



  @ register.filter(is_safe = True)
def format_description(description):
text =''
for description.i中的i.split('\n'):
text + =('< p class = site-description>'+ i +'< / p>')
返回文本



我的问题



我像这样获得原始html的输出

 < p class = site-description>模板系统分两步工作:编译和渲染。 < / p>< p class = site-description>简而言之,已编译的模板就是Node对象的列表。 < / p>< p class = site-description>因此,要定义自定义模板标签,请指定原始模板标签如何转换为Node(编译功能),以及该节点的render()方法的作用。< / p> 

而不是


模板系统通过
两步工作:编译和渲染。简单来说,已编译的模板就是
的Node对象列表。

因此,要定义自定义模板标签,
可以指定原始模板标签如何转换为Node。 (
编译函数),以及节点的render()方法的作用。




想法



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

解决方案

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



<来自django.utils.safestring的pre> import mark_safe

@ register.filter(is_safe = True)
def format_description(description):
text =''description.split('\n')中i的

text + =('< p class = site-description>'+ i +‘< / p>’)
return mark_safe(text)


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

My problem

I get the output in raw html like so

 <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>

instead of

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.

The idea

The idea is to get the text and create different paragraph for each part of the list created after the split so the text can be formatted pretty and tightty

解决方案

To disable autoescape you can use mark_safe method:

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过滤器返回将在模板中呈现的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆