在web2py中从python字符串呈现HTML,生成@usename links python

2024-09-29 20:27:50 发布

您现在位置:Python中文网/ 问答频道 /正文

web2py中的python字符串呈现HTML

我试图在一个html文件生成的web2py服务器端呈现一个锚链接

<a href="http://app/default/profile/">@username</a>

并且链接生成正确;但是当我在视图中调用它时,{{=link}}页面不会将其呈现为HTML。我试过用

^{pr2}$

以及其他各种转换。将其传递给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)

Tags: 字符串returntitle链接defhtmlmatchpage
1条回答
网友
1楼 · 发布于 2024-09-29 20:27:50

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

{{=XML(link)}}

相关问题 更多 >

    热门问题