CBV(源码分析)

from flask import Flask, views

app = Flask(__name__)

class IndexView(views.MethodView):
methods = ['GET', 'POST'] def get(self):
return '这个是get请求' def post(self):
return '这个是post请求' app.add_url_rule('/', view_func=IndexView.as_view(name='index'), endpoint='xxx') if __name__ == '__main__':
app.run()
app.add_url_rule参数
@app.route和app.add_url_rule参数:
ruel,URL规则
view_func,视图函数名称
defaults = None,默认值,当URL中无参数,函数需要参数时,使用defaults = {'k',:'v'}
为函数提供参数
endpoint = None,名称,用于反向生成URL,即:url_for("名称")
methods = None,允许请求方式,如:["GET","POST"]
# 对URL最后的/符号是否严格要求
strict_slashes = None
'''
@app.route('/index', strict_slashes=False)
#访问http://www.xx.com/index/ 或http://www.xx.com/index均可
@app.route('/index', strict_slashes=True)
#仅访问http://www.xx.com/index
'''
redirect_to = None
'''
@app.route('/index/<int:nid>', redirect_to='/home/<nid>')
'''
支持正则
'''
1. 写类,继承BaseConverter
2. 注册:app.url_map.converters
3. 使用:@app.route('/index/<regex("\d+"):nid>') 正则表达式会当作第二个参数传递到类中
'''
from flask import Flask, views, url_for
from werkzeug.routing import BaseConverter
app = Flask(import_name=__name__)
class RegexConverter(BaseConverter):
"""
自定义URL匹配正则表达式
"""
def __init__(self, map, regex):
super(RegexConverter, self).__init__(map)
self.regex = regex def to_python(self, value):
"""
路由匹配时,匹配成功后传递给视图函数中参数的值
"""
return int(value) def to_url(self, value):
"""
使用url_for反向生成URL时,传递的参数经过该方法处理,返回的值用于生成URL中的参数
"""
val = super(RegexConverter, self).to_url(value)
return val
# 添加到flask中
# regex 是我们自定义的
app.url_map.converters['regex'] = RegexConverter @app.route('/index/<regex("\d+"):nid>')
def index(nid):
print(type(nid))
print(url_for('index', nid='888'))
return 'Index' if __name__ == '__main__':
app.run()

CBV、正则的更多相关文章

  1. python 视图 (FBV、CBV ) 、Request 和Response对象 、路由系统

    一.FBV和CBV1.基于函数的view,就叫FBV(Function Based View) 示例: def add_book(request): pub_obj=models.Publisher. ...

  2. python自动化开发-[第二十天]-form表单,CBV和FBV,序列化

    1.CBV和FBV的用法 2.序列化用法 3.form表单 一.CBV和FBV 1.cbv是 class based view(基于类),fbv是function based view(基于函数) 2 ...

  3. django基础之FBV与CBV,ajax序列化补充,Form表单

    目录: FBV与CBV ajax序列化补充 Form表单(一) 一.FBV与CBV 1.什么是FBV.CBV? django书写view时,支持两种格式写法,FBV(function bases vi ...

  4. django基础2: 路由配置系统,URLconf的正则字符串参数,命名空间模式,View(视图),Request对象,Response对象,JsonResponse对象,Template模板系统

    Django基础二 request request这个参数1. 封装了所有跟请求相关的数据,是一个对象 2. 目前我们学过1. request.method GET,POST ...2. reques ...

  5. Flask 的路由系统 FBV 与 CBV

    Flask的路由系统 本质: 带参数的装饰器 传递函数后 执行 add_url_rule 方法 将 函数 和 url 封装到一个 Rule对象 将Rule对象 添加到 app.url_map(Map对 ...

  6. 巨蟒python全栈开发django6: FBV&CBV&&单表查询的其他方法

    练习CBV用法 截图中的action="/cbv/",应该是这样 上边红图,说明mysql有问题,需要重启一下 返回,输入的内容 @wrapper==>cbv=wrapper ...

  7. Web框架之Django_03 路由层了解(路有层 无名分组、有名分组、反向解析、路由分发 视图层 JsonResponse,FBV、CBV、文件上传)

    摘要: 路由层 无名分组 有名分组 反向解析 路由分发 名称空间 伪静态网页.虚拟环境 视图层 JsonResponse FBV 与 CBV(function base views与class bas ...

  8. Django 路由视图FBV/CBV

    路由层  url路由层结构 from django.conf.urls import url from django.contrib import admin from app01 import vi ...

  9. Flask - 四剑客 | templates | 配置文件 | 路由系统 | CBV

    Flask框架简介 说明:flask是一个轻量级的web框架,被称为微型框架.只提供了一个高效稳定的核心,其它全部通过扩展来实现.意思就是你可以根据项目需要进行量身定制,也意味着你需要不断学习相关的扩 ...

随机推荐

  1. ls-remote -h -t git://github.com/adobe-webplatform/eve.git 报错问题

    npm ERR! Error while executing:npm ERR! D:\开发工具\git\Git\cmd\git.EXE ls-remote -h -t git://github.com ...

  2. Springboot中登录后关于cookie和session拦截案例

    目录 一.前言 二.利用Cookie进行登录验证 一.前言 1.简单的登录验证可以通过Session或者Cookie实现. 2.每次登录的时候都要进数据库校验下账户名和密码,只是加了cookie 或s ...

  3. shell 三剑客之 awk

    awk 是shell 里的常用命令,非常强大!

  4. Android 引入第三方类库

  5. MYSQL经典练习题,熟悉DQL

    MYSQL经典练习题 (本练习题可让你熟悉DQL,快速的上手DQL) 首先,先在数据库中建立基本数据库以及表项: DROP DATABASE IF EXISTS `test`; CREATE DATA ...

  6. 标准git请求

    initCate() // 定义加载文章分类的方法 function initCate() { $.ajax({ method: 'GET', url: '/my/article/cates', su ...

  7. node-sass安装失败解决方法

    node-sass安装失败,提示如下: gyp verb check python checking for Python executable "python" in the P ...

  8. jdk1.8 新增工具类

    目录 optional 时间API Instant localDateTime LocalDate LocalTime Duration TemporalAdjuster DateTimeFormat ...

  9. 13_Python的面向对象编程-类class,对象object,实例instance

    1.面向对象概述 1.类是用来描述对象的工具,把拥有相同属性和行为的对象分为一组     2.对象是由类实例化出来的一个具体的对象         属性: 对象拥有的名词,用变量表示         ...

  10. 高可用集群corosync+pacemaker之pcs安装使用

    前文我们介绍了高可用集群corosync+pacemaker的集群管理工具crmsh的常用命令的使用,回顾请参考https://www.cnblogs.com/qiuhom-1874/tag/crms ...