从web2py
中的python字符串呈现HTML
我试图在一个html文件生成的web2py服务器端呈现一个锚链接
<a href="http://app/default/profile/">@username</a>
并且链接生成正确;但是当我在视图中调用它时,{{=link}}
页面不会将其呈现为HTML。我试过用
以及其他各种转换。将其传递给javascript并返回到页面将很好地显示链接。python字符串与html的通信不好,有什么特别的地方吗?在
在控制器中,字符串由函数调用生成:
#code barrowed from luca de alfaro's ucsc cmps183 class examples
def regex_text(s):
def makelink(match):
# The title is the matched praase @username
title = match.group(0).strip()
# The page is the striped title 'username' lowercase
page = match.group(1).lower()
return '%s' % (A(title, _href=URL('default', 'profile', args=[page])))
return re.sub(REGEX,makelink, s)
def linkify(s):
return regex_text(s)
def represent_links(s, v):
return linkify(s)
它将@username替换为指向其配置文件的链接,args(0)=username,并通过控制器调用发送到视图
def profile():
link = linkify(string)
return dict(link=link)
为了安全起见,web2py模板将自动转义通过} helper 中换行文本:
{{=...}}
插入的任何文本。要禁用转义,可以在^{相关问题 更多 >
编程相关推荐