Hardware and software Environment

  • Windows 10 64bit
  • Anaconda3 with python 3.7
  • PyCharm 2019.3
  • Flask 1.1.1

routing

What is routing

Routing is a function that maps urls and performs tasks. Use different routes to access different pages, like the/route in the previous section, to access the root of the site.

The instance

Let’s look at an example

from flask import Flask
app = Flask(__name__)

@app.route('/home')
def index() :
    return "Welcome to home!"

if __name__ == '__main__':
    app.run(host="127.0.0.1",port=5000,debug=True)
Copy the code

A 404 error occurs when you visit http://127.0.0.1:5000

Because we didn’t handle/routing code request, visit http://127.0.0.1:5000/home, will display the Welcome to the home page!

Can multiple routes, such as /home/shelly, /home/Tom, and /home/carl, be processed in the same function? The answer is yes, we can do this by passing a variable to @app.route, as follows

from flask import Flask
app = Flask(__name__)

@app.route('/home/<name>')
def index(name) :
    return f"Welcome to home!{name}"

if __name__ == '__main__':
    app.run(host="127.0.0.1",port=5000,debug=True)
Copy the code

Visit http://127.0.0.1:5000/home/carl at this time, the page will show the Welcome to the home! Carl. Visit http://127.0.0.1:5000/home/shelly, the page will show the Welcome to the home! The shelly

As you can see in the above example, the argument passed is a string, but what about other data types? Look at the following example

from flask import Flask
app = Flask(__name__)

# note that the colon after an int cannot be preceded by a space
@app.route('/home/<int:age>')
def index(age) :
    return 'Age={}'.format(age)

if __name__ == '__main__':
    app.run(host="127.0.0.1",port=5000,debug=True)
Copy the code

This time we can pass integer data, such as http://127.0.0.1:5000/home/20

Flask also provides a function called add_url_rule that does the same thing

from flask import Flask
app = Flask(__name__)

def index() :
    return 'Hello flask! '

app.add_url_rule('/'."index", index)

if __name__ == '__main__':
    app.run(host="127.0.0.1",port=5000,debug=True)
Copy the code

Visit http://127.0.0.1:5000 and you can see that the page displays Hello Flask! The effect is exactly the same as using @app.route

The template

In the previous tutorial, our pages were just displaying simple characters without using the various elements of HTML. In this section, we will start by learning Flask templates to create more complex and beautiful web pages.

Use HTML in view functions

Let’s take an example

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index() :
    return '

Hello flask!

'
if __name__ == '__main__': app.run(host="127.0.0.1",port=5000,debug=True) Copy the code

In the view function index, an HTML string is returned, with the tag

representing the first level title. Visit http://127.0.0.1:5000 to see the title version of Hello Flask!

But given the complexity of the actual web content, it’s not practical to write all the HTML in view functions, and that’s where templates come in.

Use of templates

Flask provides the Jinja2 template engine, which renders external HTML web files. The corresponding function is render_template, as shown in the following example

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index() :
    return render_template('index.html')

if __name__ == '__main__':
    app.run(host="127.0.0.1",port=5000,debug=True)
Copy the code

Go ahead and create a “templates” folder (Flask is the default place for storing HTML files, for example, js, CSS, images, etc.), create “index.html” and type in the following code

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
    <h1>Hello flask!</h1>
</body>
</html>
Copy the code

Go to http://127.0.0.1:5000 and you can see the headline is Index and the content is Hello Flask! At the same time, you can also view the source code of the web page through the browser. The content is the same as the above index.html file

Now that the HTML content is separate, how do you pass the data from the view to the template file? Consider the following example

Index. HTML file contents

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
    <h1>Hello {{ name }}!</h1>
</body>
</html>
Copy the code

The run. Py has become

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/<name>')
def index(name) :
    return render_template('index.html', name=name)

if __name__ == '__main__':
    app.run(host="127.0.0.1",port=5000,debug=True)
Copy the code

You can see that we use {{name}} in the HTML document. This is the delimiter in the Jinja2 template. The name is the data passed in by the view. Common delimiters are as follows

  • {%… %} indicates that this is a statement block
  • {{… }} gets the value of the expression
  • {#… #} stands for comment

Coming to the view section, we accept the data passed in from the URL and pass it to the HTML document through render_template

The final effect of program execution is as follows