Flask-Template-Parameter

Template
Template is a necessary module for web development. Because when we render a web page, we don't just render a plain text string, but we need to render a page with rich text tags. At this time we need to use the template. In Flask, the supporting template is Jinja2, and the author of Jinja2 is also the author of Flask. This template is very powerful and highly efficient. The following is a brief introduction to Jinja2!
Flask rendering Jinja template
To render a template, you can use the render_template method

When accessing / profile /, the profile () function will look for the index.html template file in the templates folder in the current directory. If you want to change the template file address, you should pass a keyword parameter template_folder to Flask and specify a specific path when creating the app

Create index.html profile / user.html

# @ Time : 2020/4/9 22:45
# @ Author : Ellen

from flask import Flask,render_template

# app = Flask(__name__)
app = Flask(__name__,template_folder=r"./demo")

@app.route("/")
def index():
    return render_template("index.html")
    
    以上例子将会在C盘的templates文件夹中寻找模板文件。还有最后一点是,如果模板文件中有参数需要传递,应该怎么传呢

@app.route("/profile/")
def profile():
    # 默认会从tempalates目录下面找模板文件
    return render_template("profile/user.html")


if __name__ == '__main__':
    app.run(debug=True, port=8000)

The above example introduces two ways of passing parameters. Because render_template needs to pass a keyword parameter, the first way is natural. However, when there are too many parameters to be passed in your template, it is obviously not a good choice to put all the parameters in a function, so we use a dictionary to wrap and add two * signs to convert into keyword parameters .

# @ Time : 2020/4/9 22:45
# @ Author : Ellen

from flask import Flask,render_template

app = Flask(__name__)
# app = Flask(__name__,template_folder=r"./demo")

@app.route("/")
def index():
    # return render_template("index.html",username="张胜利的幸福生活",age=18)
    context = {
        "username": "张胜利的幸福生活",
        "age": "18",
        "books": {
            "python": 33,
            "java": 23,
            "PHP": 28
    },
        "book":["python","PHP","Java"]

    }
    # username="张胜利的幸福生活"
    return render_template("index.html",**context)

@app.route("/profile/")
def profile():
    # 默认会从tempalates目录下面找模板文件
    return render_template("profile/user.html")


if __name__ == '__main__':
    app.run(debug=True, port=8000)

Create: index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">
    <h1>{{username}}</h1>
    <h2>{{age}}</h2>
   {# <h2>{{context.username}}</h2> #}
    <!--字典嵌套取值-->
    <h4>{{books.python}}</h4>
    <h4>{{books['python']}}</h4>
    <!--列表取值 可以用下标取值-->
    <h4>{{book.2}}</h4>
    <h4>{{book[1]}}</h4>
</body>
</html>
Published 118 original articles · won praise 0 · Views 2665

Guess you like

Origin blog.csdn.net/weixin_45905671/article/details/105422365
Recommended