【问题标题】:Python Flask "return" is not applying css stylePython Flask“返回”未应用 css 样式
【发布时间】:2020-05-15 23:14:58
【问题描述】:

我是 python / flask 的新手,所以我可能没有以应该解决的方式解决问题......考虑到这一点,问题是:

我有一个烧瓶 def(),它从 HTML 中获取 POST,并返回文本“文件已成功处理”。

我想要的是“文件成功处理”在我的 styles.css 文件中具有样式。

PS.:我已经有一个 index.html 可以很好地与 styles.css 配合使用。 index.html 中的代码是:

  <head>
  <link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='css/styles.css') }}">
  </head>

那么,如何将这个styles.css 应用到返回“文件已成功处理”?

@app.route("/run_ctb", methods=["GET", "POST"])
def web_run_ctb():
if request.method == "POST":
    return "<p>File successfully processed<p>"

【问题讨论】:

    标签: python css flask return


    【解决方案1】:

    该样式表目前仅适用于您的 index.html 模板。您应该为您的“/run_ctb”页面创建一个模板,例如名为 run_ctb.html,如下所示:

    <head>
        <link rel= "stylesheet" type= "text/css" href= "{{ 
        url_for('static',filename='css/styles.css') }}">
    </head>
    
    <body>
        <p>File successfully processed<p>
    </body>
    

    然后你可以让你的路由函数渲染模板:

        @app.route("/run_ctb", methods=["GET", "POST"])
        def web_run_ctb():
            if request.method == "POST":
                return render_template('run_ctb.html')
    

    【讨论】:

    • 实际上“文件已成功处理”是我将提供的十几种不同回报之一。我只是以此为例,但回报是动态的。所以我真的需要在返回中应用样式。
    • 在这种情况下,您可以将“request.method”作为参数传递给 render_template 函数。然后在您使用的任何模板中,您基本上都可以嵌入“if”逻辑,如下所述:overiq.com/flask-101/basics-of-jinja-template-language/…
    猜你喜欢
    • 1970-01-01
    • 2016-02-28
    • 2021-09-13
    • 2016-10-13
    • 2016-12-07
    • 2015-06-08
    • 1970-01-01
    • 2021-09-09
    • 2019-12-05
    相关资源
    最近更新 更多