Flask---第一个例子--使用Flask写的【Hello World !】的web程序

 1 from flask import Flask;------->引入Flask插件,pip install Flask;
 2 
 3 app=Flask(__name__) #变量app是Flask的一个实例并且必须传入一个参数,__name__对应的值是__main,即当前的py文件的文件名作为Flask的程序名称,这个也可以自定义,比如,取,'MY_ZHH_APP'
#__name__是固定写法,主要是
方便flask框架去寻找资源 ,也方便flask插件出现错误时,去定位问题

4 5 @app.route('/') #相当于一个装饰器,视图映射,路由系统生成 视图对应url,这边没有指定method .默认使用get 6 def first_flask(): #视图函数 7 return 'Hello World' #response,最终给浏览器返回的内容 8 9 10 if __name__ == '__main__': 11 app.run(debug=True) #启动这个应用服务器,并开启debug,才能定位问题

 

运行结果:

终端中显示:

 

运行过程及原理:

*当客户端访问/时,将响应hello_world()函数返回的内容。

*同理如果route('/index'),那么就是客户端访问'/index'时,将相应hello_world()函数返回的内容

*app.run()当时运行,若是服务端出问题,那么是不会在客户端展示的,但是在调试过程中,显示这个错误是有必要的,因此要加上:app.run(debug=True);

*默认情况下,Flask绑定的IP和端口分别为:127.0.0.1、5000,但实际我们可以自定义绑定,如0.0.0.0代表的是电脑的所有IP,端口80是HTTP的默认端口------------木有成功啊,为什么

1 app.run(host='0.0.0.0',port=8089,debug=True); 

注意,这不是返回Hello World!这么简单,Hello World!只是HTTP响应报文的实体部分,状态码等信息既可以由Flask自动处理,也可以通过编程来制定。

 

 

1、URL的参数

*以键值对存储,比如要查看当前URL的参数,列出URL的所有参数

1 @app.route('/index')      
2 def first_flask():    #视图函数
3     return request.args.__str__()  #返回'/index'URL的所有参数

运行结果(参数是空的):

 

 如果在地址后加上问号和参数,即可得到参数的键值对

 

 

2、浏览器传给Flask服务器的数据长什么样子(即每次在浏览器刷新一次,Flask都会接受浏览器给的数据)

@app.route('/index')    
def first_flask():
    print(request.path);------>运行结果:【/index】
    print(request.full_path);----------运行结果:【index?】
    return request.args.__str__()  #response

 

 

 

3、获取URL的某个指定的参数

1 @app.route('/index')      
2 def first_flask():

5     return request.args.get('info')----->最终reponse返回,url中info参数的值

运行结果:

*注意若URL后的参数中没有info值,那么最终执行会报错(TypeError),正常python会返回None,但是Flask不允许返回None

*解决办法1:在打印出来前判断是否存在,或是否为None

*解决办法2:r=request.args.get('info','我是代替的默认值');,判断info是否存在,如果不存在,那么,打印第二个默认值

 

V V V V

 

1     r=request.args.get('info');
2     if r==None:
3         return '不存在';
4     else:
5         return r;

 

 3、获取URL的多个参数(getlist)

@app.route('/index') 
def first_flask():

    r=request.args.getlist('info');
    if r==None:
        return '不存在';
    else:
        return str(r);------->注意最后返回的是列表类型,必现将类型转换成str,否则会报TypeError错误

 

 

 

 

4、url_for反转url:主要处理页面重定向,URL的获取),要导入url_for

 1 @app.route('/index')
 2 def index():
 3     print(url_for('my_layout',username='aaa'));------>url_for的第一个参数是视图方法名,比如my_layout,hello_world,
 4     print(url_for('hello_world',username='bbb'));----->第二个参数是参数,如有没有可以不写,如有有参数,必须要写(有几个写几个),<参数名>='给值'
 5     return 'index';
 6 
 7 #layOut.html
 8 
 9 @app.route('/hello/<username>')
10 def hello_world(username):
11     return render_template('hello.html',username=username);
12 
13 
14 @app.route('/layout/<username>')
15 def my_layout(username):
16     return render_template('jicheng.html',username=username);

 

最终看运行结果,可以看到打印出该视图方法的URL地址

 

 

4、url_for的页面重定向小例子

@app.route('/hello/<username>')
def hello_world(username):
    return render_template('hello.html',username=username);


@app.route('/layout/<username>')
def my_layout(username):
    return render_template('jicheng.html',username=username);


@app.route('/login/<is_login>')
def login(is_login):
    if is_login==1:------------------------>判断is_login是否等于1,如果是,则跳转到【my_layout】视图所对应的页面
        return redirect(url_for('my_layout',username=is_login));
    else:
        return redirect(url_for('hello_world',username='你还没登陆呢,请先登陆'))----------如果不是,则跳转到【hello_world】视图所对应的页面

if __name__ == '__main__':
    app.run(debug=True);

 

posted @ 2019-03-13 10:31  littlepoemers_23ujhs  阅读(2836)  评论(0编辑  收藏  举报