【问题标题】:I want to return html in a flask route [duplicate]我想在烧瓶路线中返回 html [重复]
【发布时间】:2018-04-28 03:09:12
【问题描述】:

我想使用 html('<h1>content</h1>') 之类的东西,而不是使用 send_static_file,有点像 jsonify。

@app.route('/incorrect_pass')
def incorrect_pass():
    return html('Incorrect password. <a href="/">Go back?</a>')

是我想做的。

【问题讨论】:

  • 只做return 'Incorrect password. &lt;a href="/"&gt;Go back?&lt;/a&gt;'。 HTML 是默认的

标签: python flask


【解决方案1】:

没关系。应该查的Flask 以 html 形式返回字符串,因此上面不需要 html()

【讨论】:

  • 是的 - 与我的答案的唯一区别是您可以修改头部(所以样式表等)
【解决方案2】:

为什么不只是:

def html(content):  # Also allows you to set your own <head></head> etc
   return '<html><head>custom head stuff here</head><body>' + content + '</body></html>'

@app.route('/incorrect_pass')
def incorrect_pass():
    return html('Incorrect password. <a href="/">Go back?</a>')
    # OR return  'Incorrect password. <a href="/">Go back?</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 2016-03-11
    • 2018-11-02
    • 2020-11-18
    • 1970-01-01
    • 2020-02-07
    • 2018-03-26
    相关资源
    最近更新 更多