Flask-one, flask
Installation:
1 pip install Flask
Start the first script: python hello. py access address: http: // 127.0.0.1: 5000. The script is as follows:
1 from flask import Flask2 app = Flask (_ name _) 3 4 @ app. route ('/') # route specifies 5 def hello_world (): 6 return 'Hello worlds summerkxy' 7 if _ name _ = '_ main __': # The application will be started only when this example is used. If this file is included, the application 8 app will not be started. debug = True # enable debug mode 9 app. run ()
App. debug = True to enable the debugging mode. You can also add parameters to app. run (debug = True). Advantage: you do not need to restart the application after modifying the program. This is the simplest application.
1 from flask import Flask 2 app = Flask (_ name _) 3 4 # first Route 5 @ app. route ('/') 6 def hello_world (): 7 return 'Hello worlds summerkxy' 8 9 # The second route 10 @ app. route ('/summerkxy') 11 def summer (): # The function name is random and cannot conflict with the built-in function name; 12 return "Hello summerkxy" 13 if _ name _ = '_ main _': 14 app. debug = True15 app. run ()
Url with Parameters
1 @app.route('/show/<username>')2 def show_user(username):3 return "show %s" % username
Multi-parameter transfer:
1 @app.route('/show/<username>/<age>')2 def show_user(username,age):3 return "show "+username+" age:"+age
The parameter has the following types:
@app.route("/age/<int:age>")def show_age(age): return "show age %d" % age
There are three types of converters:
Int |
Float |
Path |
Integer |
Integer or floating point type |
Common type, with diagonal lines |
Comparison of url uniqueness:
1 @app.route("/about/")2 def show_about():3 return "about/"4 5 @app.route("/about")6 def show_about1():7 return "about"
In fact, it will only match the first url to construct the url url_for () # unknown according to the request method, call different functions, and then return different content:
1 from flask import Flask, url_for # url_for is temporarily unavailable to 2 from flask import request 3 app = Flask (_ name _) 4 5 @ app. route ("/") 6 def index (): 7 return "index" 8 9 @ app. route ('/login', methods = ['get', 'post']) # first, it will be determined based on the elements in the methods list. If no value is found, an error is reported. Method 405 is not allowed; 10 def login (): 11 if request. method = 'post': 12 return do_the_login () 13 else: 14 return show_the_login_form () 15 def do_the_login (): 16 return "POST" 17 def show_the_login_form (): 18 return "else method" 19 20 if _ name _ = '_ main _': 21 app. debug = True22 app. run ()
Introduce static css and js files
1 @ app. route ("/hello") 2 def hello (): 3 cssurl = url_for ('static ', filename='style.css') 4 return render_template ("hello.html", age = cssurl) 5. 6 <link type = "text/css" rel = "stylesheet" href = "{age }}"> 7 Question: A single url returns a style, which is meaningless. How to pass multiple values and use list/dist to pass parameters to the template file:
1 @ app. route ("/hello") 2 def hello (name = 'summerkxy'): 3 return render_template ("hello.html", age = name) # The variable name age is passed to the template file. 4. html file 5