【问题标题】:'html template not found' error (VS Code)“找不到 html 模板”错误(VS 代码)
【发布时间】:2020-08-27 00:00:37
【问题描述】:

我在 VS 代码上有以下文件,当我尝试在浏览器上运行它们时,它根本无法识别出任何 reg.html 模板。有什么问题?

python 文件

from flask import  Flask, 

render_template, flash

app = Flask(__name__)
app.config["SECRET_KEY"] = "asdfghjkl"

@app.route("/")
def form():
    return render_template("reg.html", title = "Form")

if __name__ == "__main__":
    app.run(debug = True)

html 文件 (reg.html)

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login/Signup - ((title)) </title>

<!-- Bootstrap CSS-->
<link rel = "stylesheet" href = "(( url for('static', filename = 'css/bootstrap.min.css)' ))">
<link rel = "stylesheet" href = "(( url for('static', filename = 'css/site.css') ))">
 
</head>
<body>
    <div class = "container">
        <div class = "row">
            <div class = "col-md-5">
                <h1 class = "text-center">Registration form</h1>
        </div>
        <div class = "col-md-2"></div>
        <div class = "col-md-2"></div>
    </div>
</div>

<!-- Javascript -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.js.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src = "((url for ('static', filename = 'js/bootstrap.min.js')))">`</script>`

</body>
</html>

【问题讨论】:

  • 为什么render_template, flash不在第1行(from
  • @rioV8 哦,嗯,这可能是我粘贴抱歉时发生的,但它在代码的第 1 行

标签: python html python-3.x templates visual-studio-code


【解决方案1】:

为了解决这个问题,创建一个名为templates的文件夹,这是Flask默认查找模板的地方,然后将reg.html放在这个文件夹中:

启动程序,你会观察到结果:

flask的详细资料,可以参考Flask Tutorial

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2020-07-10
    • 2021-12-21
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多