works:programmer:py:flask-forms
Работа с формами из Flask
from flask import Flask, request from flask import abort, redirect, url_for #app = Flask('app') app = Flask(__name__) @app.route('/') def hello_world(): return """Hello, World! <a href="/exit?page=8&filter=18&required=abc">Test</a> <form method="POST" action="/form"> <input type="text" name="name"> <input type="text" name="surname"> <input type="submit"> </form>""" @app.route('/exit', methods=['GET']) def exit_world(): page = request.args.get('page', default = 1, type = int) filter = request.args.get('filter', default = '*', type = str) required = request.args['required'] return("Exit Page " + str(page) + ", filter=" + filter + " req=" + required) @app.route('/form', methods=['POST']) def formTest(): if request.method == 'POST': name = request.form['name'] surname = request.form.get('surname') if surname == None: surname = "" return '<h1>Hello '+name+' '+surname+'</h1><a href="/">back</a>' else: hello_world() @app.route("/json_post") def json_test(): # {"language":"Python","primary":{"version":3.8},"arr":[1,2,3]} json = request.get_json() lang = json["language"] version = json["primary"]["version"] # arr1 = json["arr"][0] nonrequiredValue = None if 'nonrequiredValue' in json: nonrequiredValue['nonrequiredValue'] return "Language {} and version {}" % (lang, version) @app.route('/') def index(): return redirect(url_for('login')) @app.route('/login') def login(): abort(401) return "this_is_never_executed" @app.errorhandler(404) def page_not_found(error): return render_template('page_not_found.html'), 404 if __name__ == '__main__': app.run(host='0.0.0.0', port=8080, debug=None)
works/programmer/py/flask-forms.txt · Последнее изменение: 2020/08/24 15:46 — 127.0.0.1