Record#
- Today I learned the method of receiving variables in the request: get.
- The get method is passed through URL parameters, and in the code, request.args is used to access all the variables and their values passed through the URL.
- The receiving method still needs to be defined in the route definition: methods=['GET']
- There are two differences from post:
- In the route definition, one method is for post and the other is for get.
- In the code, one variable is assigned form.request and the other is assigned request.args.
- Today's exercise is to pass variables through the URL and display a page in two languages.
CODE#
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=["GET"])
def index():
data = request.args
if data["lang"].lower() == "en":
return "Hello English"
elif data["lang"].lower() == "cn":
return "你好 中国话"
app.run(host='0.0.0.0', port=81)