【问题标题】:python (flask) how to render html links from plain text inputpython(flask)如何从纯文本输入中呈现html链接
【发布时间】:2019-05-14 09:43:25
【问题描述】:

我在工作流程中使用 python、flask、sqlalchemy/sqllite、jinja。

我有一堆文本存储在数据库中,它有纯文本段落,其中的文本将是 email@address.com、somedomain.com、www.somedomain.com、https://somedomain.com、@987654322 之类的内容@等。

是否存在一种解决方案,用于在我加载数据时解析数据并在我绘制到浏览器时将其呈现为 html 链接?目前它只是呈现为文本而没有链接处于活动状态。

非常感谢

【问题讨论】:

    标签: python html sqlite jinja2 flask-sqlalchemy


    【解决方案1】:

    一个快速的解决方案是使用为此类任务构建的现有库python-textile

    例如:

    import textile
    from flask import Flask, render_template
    
    app = Flask(__name__)
    
    @app.route("/")
    def index():
    
        plain_text = 'This text contains a link to https://stackoverflow.com'
        marked_up_text = textile.textile(plain_text)
    
        return render_template('layout.html', body=marked_up_text)
    

    layout.html

    {{body | safe}}
    

    【讨论】:

    • 谢谢你,看起来很有希望。我试着试一试并得到以下错误。我猜图书馆可能有问题? /venvLinux/lib/python3.6/site-packages/textile/core.py”,第 228 行,解析中 if text.strip() == '': AttributeError: 'Post' object has no attribute 'strip'跨度>
    • @robster 您的 Post 对象不是 str
    • 谢谢!今晚我会调查一下。
    猜你喜欢
    • 1970-01-01
    • 2023-01-20
    • 2020-09-17
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多