【发布时间】:2015-05-15 11:36:57
【问题描述】:
从web2py中的python字符串渲染HTML
我正在尝试在 web2py 中生成服务器端的 html 文件中呈现锚链接
<a href="http://app/default/profile/">@username</a>
并且链接生成正确;但是,当我在我的视图{{=link}} 中调用它时,页面不会将其呈现为 HTML。我试过使用
mystring.decode('utf-8')
和其他各种转换。将其传递给 javascript 并返回到页面显示链接正常。与 html 不能很好沟通的 python 字符串有什么特殊之处吗?
在控制器中,字符串由函数调用生成:
#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)
【问题讨论】:
-
你能把你的代码贴在控制器里吗?
标签: javascript jquery python html web2py