zoukankan      html  css  js  c++  java
  • flask之CBV模式

    flask_cbv.py

     1 '''
     2 flask中的CBV模式:
     3     (1)导入views模块: from flask import views
     4     (2)定义类,继承views.MethodView类: class 类名(views.MethodView)
     5     (3)在类中定义函数名为允许的请求方式的小写形式,进行函数定义,如def get(self):...
     6     (4)添加路由规则:
     7         CBV:app.add_url_rule(rule,endpoint='',view_func=类名.as_view(name=''))
     8         FBV:app.add_url_rule(rule,endpoint='',view_func=函数名))(直接替代@app.route()方式)
     9 
    10         参数:
    11             rule 请求路径
    12             endpoint设置mapping路由映射函数rule:{endpoint:func}
    13             view_func路由映射的视图函数或者类(as_view()中的name参数设置当前路由映射函数名,唯一指定,不设置endpoint自动为name值)
    14     (5)methods=[]类中设置属性,添加允许的请求方式,不写默认都支持
    15     (6)decorators = []对类中的函数都加上装饰器,列表中可以放多个装饰器函数名,以此执行
    16 
    17 '''
    18 from flask import Flask, views, render_template, send_file, request, session
    19 
    20 app=Flask(__name__)
    21 app.secret_key='wertyui234567gf!@#$%^&*('
    22 
    23 class Login(views.MethodView):
    24     # methods = ['get', 'post', 'head', 'options','delete', 'put', 'trace', 'patch']#可以在类中指定允许的请求方式,不指定会根据定义的函数进行选择
    25     decorators = []#对类中的函数都加上装饰器,列表中可以放多个装饰器函数名,以此执行
    26     def get(self):
    27         return render_template('login.html')
    28     def post(self):
    29         username=request.form.get('username')
    30         pwd=request.form.get('pwd')
    31         if username == 'yang' and pwd == '123456':
    32             session['username'] = 'yang'
    33             return '登录成功!'
    34         else:
    35             return '账号或密码有误!!!!'
    36 #路由映射视图函数
    37 app.add_url_rule('/login',view_func=Login.as_view(name='login'))
    38 
    39 if __name__ == '__main__':
    40     app.run()

    lohin.html

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>logine</title>
     6 </head>
     7 <body>
     8 <form action="" method="post">
     9     用户名:<input type="text" name="username">
    10     密码:<input type="password" name="pwd">
    11     <input type="submit" value="提交">
    12 </form>
    13 </body>
    14 </html>
  • 相关阅读:
    sql 中 列转换成拼音首字母简写【邹建版】
    取一个任意数所有 和的等式
    sql 汉字转全拼音(非首字母)
    实现消息来时让网页标题闪动
    hdoj 1754 I Hate It 线段树(二)
    nyoj 247 虚拟城市之旅 路径压缩
    hdoj 1247 字典树分词 strncpy函数
    hdoj 1671字典树水题之三 静态数组节约内存法
    sort函数
    hdoj 1166 排兵布阵 线段树()
  • 原文地址:https://www.cnblogs.com/open-yang/p/11178069.html
Copyright © 2011-2022 走看看