闲话不说,进入正题.
打开你的Pytharm,然后新建一个项目,这里你直接选中Flask模板,一切都帮你搞定了. 我们是快速入门,所以不要折腾一些命令行以及其它虚拟环境.
然后这个项目就可以直接运行,然后你就可以看到经典的Hello world这里使用app.route来管理接口服务的路由,下面我都对一些方法进行了说明,可以看图
这里我提炼出几点
每次修改程序后,需要先停止服务,然后再重新运行服务
Flask默认的请求方式是GET
怎么接受请求的参数
这里可以直接通过后缀变量来传入数据
@app.route('/search/<keyword>') def search(keyword): '''尖括号里面可以输入变量字符串''' return 'hello world %s' % keyword
当然也可以通过这种方式来请求,这里我是演示get请求方式,这里需要先专稿Flask中的Request模块,然后才能使用
print('打印全部参数:%s' % request.args) # 获取请求字段one,two one = int(request.args.get('one')) two = int(request.args.get('two'))
怎么进入到指定的网页
需要先导入Flask中的render_templete模块,然后再使用return render_template('index.html')这个index.html文件在你的Templates模板文件夹中
怎么跳转到其它网页,以及如何带参数,带几个参数
return render_template('result.html', arg1=one, arg2=two, mode_word=mode_word, show=result)这里是跳转到result.html网页中,后面是我们传入的参数,有几个参数我们就可以传入几个参数
结果
完整代码
from flask import Flask from flask import request from flask import render_template app = Flask(__name__) @app.route('/') def hello_world(): '''打开浏览器默认是进入这个方法''' # return 'Hello World!' return render_template('index.html') @app.route('/index') def index(): '''后面追加index是进入这个方法''' return 'hello world index' @app.route('/search/<keyword>') def search(keyword): '''尖括号里面可以输入变量字符串''' return 'hello world %s' % keyword @app.route('/plus') def doPlus(): return '666' @app.route('/plus') def doPlus2(name): return '777' @app.route('/add') def doAdd(): print('打印全部参数:%s' % request.args) # request.args 就是全部参数的字典 if 'one' not in request.args: return '没有请求参数one' if 'two' not in request.args: return '没有请求参数two' try: one = int(request.args.get('one')) two = int(request.args.get('two')) result = '结果是:%d' % (one + two) return render_template('result.html', show=result) except Exception as e: result = '错误信息:%s' % e.args return render_template('result.html', show=result) @app.route('/mul') def doMul(): print('打印全部参数:%s' % request.args) # request.args 就是全部参数的字典 if 'one' not in request.args: return '没有请求参数one' if 'two' not in request.args: return '没有请求参数two' try: one = int(request.args.get('one')) two = int(request.args.get('two')) result = '结果是:%d' % (one * two) return render_template('result.html', arg1=one, arg2=two, show=result) except Exception as e: result = '错误信息:%s' % e.args return render_template('result.html', show=result) @app.route('/comp') def doComp(): print('打印全部参数:%s' % request.args) # request.args 就是全部参数的字典 if 'one' not in request.args: return '没有请求参数one' if 'two' not in request.args: return '没有请求参数two' try: one = int(request.args.get('one')) two = int(request.args.get('two')) mode = request.args.get('mode') result = 0 mode_word = '' if '+' == mode: result = one + two mode_word = '加法' elif '-' == mode: result = one + two mode_word = '减法' elif '*' == mode: result = one * two mode_word = '乘法' elif '/' == mode: result = one / two mode_word = '除法' else: result = '计算方式无效' mode_word = '计算方式无效' result = '结果是:%s' % str(result) return render_template('result.html', arg1=one, arg2=two, mode_word=mode_word, show=result) except Exception as e: result = '错误信息:%s' % e.args return render_template('result.html', show=result) if __name__ == '__main__': app.run()
完整代码加群:850591259