flask的基础认识

刚开始学习flask基础知识,有了一点点的认识,所以在此大概写一下自己的理解,详细步骤和功能在代码段介绍:

from flask import Flask,render_template,request,redirect
app = Flask(__name__)
#下面为Flask里的参数
#static_url_path,这跟用户请求进来的静态路径进行匹配。 #static_folder,这是设置静态文件在服务器上的文件路径。 #template_folder,这是配置模板文件在服务器上的路径 #
'/' --->首页 @app.route('/') def hello_world(): return 'Hello World!' # 在这里的methods为请求方式,是判断路由html页面响应请求的方式, # 根据不同的方式返回不同的结构 @app.route('/login',methods=['get','post']) #print(request)#request请求对象,里面具有用户请求的详细信息 #获取表单数据(POST方法) print(request.form) #获取表单数据(GET方法) print(request.args) print(request.__dict__['environ']['REQUEST_METHOD']) def login(): #表示返回的页面为当前路劲下的templates文件夹下的login.html,
   #此处的render_template()为固方法模式
return render_template('login.html') if __name__ == '__main__': #让服务器,应用运行起来。 #当前服务器的所有IP地址都可以用host='0.0.0.0' #debug=True开启调试模式,修改保存以后,会自动重新运行,并且会将错误直接显示在HTML页面上 app.run(host='0.0.0.0',port=80,debug=True)

下面是以上py文件调用的login.html页面代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <h1>点餐系统</h1>
    <!-- 这是一个form表单,登录提交后,会跳转到/login/123/enter,采用的方式是post
    ,还有另一种方式,是get,一般用post,如果不写则默认为get 
    此处必须标明请求方法,否则无法请求到数据method='post'
-->
    <form action="/login/123/enter",method='post' >
    <input type="text" name="user_name" value="" placeholder="请输入你的登录名">
    <input type="text" name="password" value="" placeholder="请输入你的密码">
    <button type="submit">登录</button>
    </form>    
    <!-- 表示插入当前路径文件夹static下的图片(静态图片) -->
    <img src="/static/11.jpg" alt="">
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/Dark-fire-liehuo/p/9852883.html