<> use Python Of flask Build a simple framework web website

<>1.flask The simplest framework , return hello world:
from flask import * app = Flask(__name__) @app.route('/') def hello_world():
return 'Hello World!' if __name__=="__main__": app.run(host='0.0.0.0',port=8080)
#0.0.0.0 The address can be given to all users of this machine IP Address access
* We can do it here app On the route hello_world Function to modify the returned ‘hello
world!’ To change the information returned , Again, we're in the last line of code 0.0.0.0 The function of is to allow others to connect to the machine IP Address can be used to access the service , For example, you can enter the address in the browser :127.0.0.1:8080 Access .
<>2. Give Way flask You can return a simple web page to the browser , The view function is added , Enrich the web :

* Create one in the same project file directory templates Folder for , Create one in this directory html file , Also named index.html. <!DOCTYPE
html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Wow</title> </head>
<body> <h1> I'm super handsome !</h1> </body> </html>
* At this point, we need to be in the back end flask Add this to the routing distribution in the framework html Document distribution rules and tasks , So that the browser can access the web page through a certain address .
* Let's go through step one flask Add a routing distribution task to the back end , Update as follows : from flask import Flask,render_template,
request app= Flask(__name__) @app.route('/') def hello_world(): return 'Hello
World!' @app.route('/index') def index(): return render_template('index.html')
# I'll find it automatically templates It's in the folder index.html, There is no need for an absolute path . if __name__=="__main__": app.run(host
='0.0.0.0',port=8080) # 0.0.0.0 The address can be given to all users of this machine IP Address access
<>3. In the same way, we can make flask Add more of what we need web Webpage :

* 1. Add routing distribution task to back end : from flask import Flask,render_template,request app = Flask(
__name__) @app.route('/') def hello_world(): return 'Hello World!' @app.route(
'/index') def index(): return render_template('index.html')
# I'll find it automatically templates It's in the folder index.html, There is no need for an absolute path . @app.route('/login') def index():
return render_template('login.html') @app.route('/content') def index(): return
render_template('content.html') if __name__=="__main__": app.run(host='0.0.0.0',
port=8080)
* 2. New front end , stay templates Add under directory login.html,content.html Two html file // login.html <!
DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title
> </head> <body> <form action="/login_finsh" method="POST"> user name <input type=
"text" name="username" id=""> password <input type="text" name="password" id=""> <
input type="submit" value=" Sign in "> </br> <input type="button" value="open1"> </br>
<button type="button" onclick="open_function()"> open1</button> </form> </body>
</html> // content.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=
"UTF-8"> <title>Title</title> </head> <body> <p> This is the content page !</p> </body> </html>
* 3. Only when we type the corresponding address in the browser address bar, we can browse these web pages .

Technology
©2019-2020 Toolsou All rights reserved,
Huawei 2021 session Hardware Engineer Logical post (FPGA) Super detailed surface !!!Vue-element-admin upgrade ui edition virtual machine VMware Download and install the most detailed tutorial !C++ Move constructor and copy constructor sound of dripping water java Backstage interview pygame Realize full screen mode and adjustable window size mysql Database setting character set configuration modification my.ini file (windows)30 What's the experience of being a junior programmer at the age of 20 C++ Multithreading programming ( Summary of common functions and parameters )python_ cherry tree