在 web2py
中从 python 字符串渲染 HTML
我正在尝试在 web2py 中生成的服务器端的 html 文件中渲染 anchor 链接
<a href="http://app/default/profile/">@username</a>
并且链接正确生成;但是,当我在 View 中调用它时 {{=link}}
页面不会将其呈现为 HTML。我尝试过使用
mystring.decode('utf-8')
以及各种其他转换。将其传递给 JavaScript 并返回页面可以很好地显示链接。 python 字符串是否有一些特定的地方不能与 html 很好地通信?
在 Controller 中,字符串是由函数调用生成的:
#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) = 用户名,并通过 Controller 调用发送到 View
def profile():
link = linkify(string)
return dict(link=link)
最佳答案
为了安全起见,web2py 模板将自动转义通过 {{=...}}
插入的任何文本。要禁用转义,您可以将文本换行在 XML()
helper 中。 :
{{=XML(link)}}
关于javascript - 从 web2py 中的 python 字符串渲染 HTML,生成 @usename 链接 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29024590/