【问题标题】:Rendering HTML from a python String in web2py, generate @usename links python从web2py中的python字符串渲染HTML,生成@usename链接python
【发布时间】: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


【解决方案1】:

为安全起见,web2py 模板会自动转义通过{{=...}} 插入的任何文本。要禁用转义,您可以将文本换行在XML() helper

{{=XML(link)}}

【讨论】:

  • 谢谢,如此简单,却又如此令人沮丧。这对我有用!
猜你喜欢
  • 1970-01-01
  • 2021-04-19
  • 2021-01-30
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 2014-05-23
  • 2011-02-12
  • 1970-01-01
相关资源
最近更新 更多