使用url_for()时,会自动调用转换器的to_url()方法

15811111111 修" /> 使用url_for()时,会自动调用转换器的to_url()方法 - 安迪9468 </span> <span class="icon-clock-1"> 2024-09-27 09:33:56 </span> <span class="icon-article-1"> <a href="/link/RTM1cHJRQUV6dg==" target="_blank" rel="noindex,nofollow,oarchive">原文</a> </span> </div> <div class="post-content"> <div id="single_top"></div> <script>insertAdIfNeeded("single_top");</script> <p>视图反推url,在动态url(转换器)反推中的应用</p> <p><img referrerpolicy="no-referrer"src="https://img2018.cnblogs.com/blog/552284/201905/552284-20190515201911545-1005440740.png" /></p> <div> <pre class="prettyprint linenums"># -*- coding: utf-8 -*-<br /> from flask import Flask, url_for, redirect<br /> from werkzeug.routing import BaseConverter app = Flask(__name__) # 转换器<br /> # 127.0.0.1:5000/goods/123<br /> @app.route("/goods/<int:goods_id>")<br /> # @app.route("/goods/<goods_id>") # 不加转换器类型, 默认是普通字符串规则(除了/的字符)<br /> def goods_detail(goods_id):<br /> """定义的视图函数"""<br /> return "goods detail page %s" % goods_id # 1. 定义自己的转换器<br /> class MobileConverter(BaseConverter):<br /> def __init__(self, url_map):<br /> super(MobileConverter, self).__init__(url_map)<br /> self.regex = r'1[34578]\d{9}' class RegexConverter(BaseConverter):<br /> """""" def __init__(self, url_map, regex):<br /> # 调用父类的初始化方法<br /> super(RegexConverter, self).__init__(url_map)<br /> # 将正则表达式的参数保存到对象的属性中,flask会去使用这个属性来进行路由的正则匹配<br /> self.regex = regex def to_python(self, value):<br /> """"""<br /> print("to_python方法被调用")<br /> # return "abc"<br /> # value是在路径进行正则表达式匹配的时候提取的参数<br /> return value def to_url(self, value):<br /> """使用url_for的方法的时候被调用"""<br /> print("to_url方法被调用")<br /> return "15811111111"<br /> # return value # 2. 将自定义的转换器添加到flask的应用中<br /> app.url_map.converters["re"] = RegexConverter<br /> app.url_map.converters["mobile"] = MobileConverter # 127.0.0.1:5000/send/18612345678<br /> # @app.route("/send/<mobile:mobile_num>")<br /> @app.route("/send/<re(r'1[34578]\d{9}'):mobile_num>")<br /> def send_sms(mobile_num):<br /> return "send sms to %s" % mobile_num @app.route("/index")<br /> def index():<br /> url = url_for("send_sms", mobile_num="18922222222")<br /> # /send/18922222222<br /> return redirect(url) if __name__ == '__main__':<br /> # 通过url_map可以查看整个flask中的路由信息<br /> print(app.url_map)<br /> # 启动flask程序<br /> app.run(debug=True) </pre> </div> <p>  </p> <p>访问:<a href="http://127.0.0.1:5000/index" rel="external nofollow noreferrer">http://127.0.0.1:5000/index</a></p> <p>跳转:<a href="http://127.0.0.1:5000/send/15811111111" rel="external nofollow noreferrer">http://127.0.0.1:5000/send/15811111111</a></p> <p><img referrerpolicy="no-referrer"src="https://img2018.cnblogs.com/blog/552284/201905/552284-20190515202325701-1214262762.png" /></p> <p>修改to_url后</p> <p><img referrerpolicy="no-referrer"src="https://img2018.cnblogs.com/blog/552284/201905/552284-20190515202413868-213892806.png" /></p> <p><img referrerpolicy="no-referrer"src="https://img2018.cnblogs.com/blog/552284/201905/552284-20190515202442952-1172779095.png" /></p> <div id="single_bottom"></div> <script>insertAdIfNeeded("single_bottom");</script> <h2><a href="/R/E35prQAEzv/">使用url_for()时,会自动调用转换器的to_url()方法的更多相关文章</a></h2> <ol> <li><a href="https://www.shuzhiduo.com/A/8Bz8oN06dx/">thinkphp模型中的获取器和修改器(根据字段名自动调用模型中的方法)</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">thinkphp模型中的获取器和修改器(根据字段名自动调用模型中的方法) 一.总结 记得看下面 1.获取器的作用是在获取数据的字段值后自动进行处理 2.修改器的作用是可以在数据赋值的时候自动进行转换处 ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/QV5ZqmyJyb/">EditText取消自动调用键盘事件(方法之一)</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">直接上代码,这只是其中一种方法: 重点在于是在该EditText的父空间中设置 <LinearLayout android:layout_width="match_parent&quo ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/amd08pDWdg/">jQuery在页面加载的时候自动调用某个函数的方法</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">第一种:$(document).ready(function(){ func(xxx)//执行函数}); 第二种:$(function(){ func(xxx)//执行函数}); 第三种:jQuery ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/Ae5RV1EL5Q/">vue中使用vue-router切换页面时滚动条自动滚动到顶部的方法</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">原文:http://www.jb51.net/article/129270.htm main.js入口文件配合vue-router写这个 router.afterEach((to,from,next) ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/x9J22V8NJ6/">CocoaPods中的头文件import导入时不能自动补齐的解决方法</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">1.选择target(就是左边你的工程target)-->BuildSettings-->search Paths下的User Header Search Paths 2.添加“$(POD ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/n2d9XYeQ5D/">PHP中 对象自动调用的方法:__set()、__get()、__tostring()</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">总结: (1)__get($property_name):获取私有属性$name值时,此对象会自动调用该方法,将属性name值传给参数$property_name,通过这个方法的内部 执行,返回我们传 ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/qVdeOAWbJP/">C++中构造函数的手动和自动调用方式</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">1,对象的构造通过构造函数来完成,和类名相同且没有返回值,这个时候只有参   数一个特性,构造函数可以自定义参数,这个参数一般而言就是对类进行初始  化来使用的:带有参数的构造函数的意义在于可以使得每 ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/QV5ZnwP65y/">vue+element-ui, el-upload组件 文件上传之前return false,会自动调用文件移除回调问题</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">日常搬砖的时候,项目中在使用element-ui的上传组件,但是当我在文件上传文件之前的回调里面做了些文件格式的二次校验和文件大小的校验的时 然后 return false 会发现调用 文件移除的回调 ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/B0zq2xwNJv/">原!! java直接打印一个对象时,并不是直接调用该类的toString方法 ,而是会先判断是否为null,非null才会调用toString方法</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">网上看了好多java直接打印一个对象时,直接调用该类的toString方法 . 但是: Object obj=null; System.out.println(obj);//没有报错 System.o ...</p> </li> </ol> <h2>随机推荐</h2> <ol> <li><a href="https://www.shuzhiduo.com/A/mo5k4DV2dw/">nginx利用fastcgi_cache模块缓存</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">nginx不仅有个大家很熟悉的缓存代理后端内容的proxy_cache,还有个被很多人忽视的fastcgi_cache.proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和 ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/WpdKGLRNdV/">C# 后台POST和GET 获取数据</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">C# 后台POST和GET 获取数据 , data.Length);     newStream.Close();     HttpWebResponse myResponse = (HttpWebR ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/obzbgXGVJE/">(三)Asp.net web api中的坑-【http post请求中的参数】</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">接上篇, HttpPost 请求 1.post请求,单参数 前端 var url = 'api/EnterOrExit/GetData2';var para = {};para["Phone ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/pRdBxWQPJn/">Linux 安装环境初始化检查 安装Nginx</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">一 .阿里云 centos 6.8 32 位裸环境 实现:Linux Nginx mysql php redis 查看当前安装的服务 [root@iZgahlk1l73998Z etc]# servi ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/nAJvamZGzr/">Dapper连接MySql数据库</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">首先通过NuGet在项目中分别安装Dapper和MySql.Data的引用 PM> Install-Package Dapper -Version PM> Install-Package ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/gGdXvGOYJ4/">Redis内存数据库的基本语法</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">Redis: - nosql数据库,非关系型数据库 - 支持5大数据类型 (字符串String,列表list.字典hash,集合set,zset) - 与之相似的有memcache,但memcache ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/xl56nrL45r/">nginx rewrite 规则</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">rewrite功能:使用nginx提供的全局变量或自设定的变量,结合正则表达式和标志位实现url重写以及重定向. 配置域:server,localtion. flag标志位     last : 相当 ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/kPzO4X43Jx/">Java中的静态导入</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">Java从1.5开始,增加了静态导入的语法,静态导入使用import static语句,分为两种: 导入指定类的某个静态成员变量.方法. 导入指定类的全部的静态成员变量.方法. 下面是代码演示: /* ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/o75NEXwWJW/">Excel逻辑运算函数</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">Excel逻辑运算函数 1.FALSE和TRUE的使用 ​ 筛选出表中salary>6.gender为男.age>28至少满足这三个条件中的两个的数据 1.依次使用:=C2>6.=D ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/amd0284jJg/">asp.net mvc抓取微信文章里面所有的图片</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">/// <summary> /// 下载指定URL下的所有图片 /// </summary> public class WebPageImage { /// <summa ...</p> </li> </ol> </div> <!--<div class="shareBox clearfix"> </div>--> <!--<div class="posts-cjtz content-cjtz clearfix"><img src="/images.grace/2865270162.png" alt="" /></div> --> <div class="next-prev-posts clearfix"> </div> </div> <div class="clear"></div> </div> <div class="widget"><h3><span>热门专题</span></h3> <div class="widge_tags"> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/ulink2-%e6%89%be%e4%b8%8d%e5%88%b0/" title="ulink2 找不到">ulink2 找不到</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/centos%e5%ae%89%e8%a3%85mysql%e9%80%89%e6%8b%a9%e5%93%aa%e4%b8%aa%e7%b1%bb%e5%9e%8b/" title="centos安装mysql选择哪个类型">centos安装mysql选择哪个类型</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/%e5%88%86%e5%b8%83%e5%bc%8f%e4%ba%8b%e5%8a%a1%e4%b8%80%e8%87%b4%e6%80%a7%e5%ae%9e%e7%8e%b0/" title="分布式事务一致性实现">分布式事务一致性实现</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/nodejs%e8%bf%90%e8%a1%8c%e5%8e%9f%e7%90%86/" title="nodejs运行原理">nodejs运行原理</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/postman%e4%bd%bf%e7%94%a8%e6%8e%a5%e5%8f%a3%e8%8e%b7%e5%8f%96%e5%88%b0token%e4%bf%9d%e5%ad%98%e8%b5%b7%e6%9d%a5%e5%90%8e%e7%bb%ad%e4%bd%bf%e7%94%a8/" title="postman使用接口获取到token保存起来后续使用">postman使用接口获取到token保存起来后续使用</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/js%e4%b8%ad-echar%e7%bb%98%e5%88%b6%e6%9f%b1%e7%8a%b6%e5%9b%be%e5%ae%9e%e7%8e%b0%e9%92%bb%e5%8f%96%e5%8a%9f%e8%83%bd/" title="js中,echar绘制柱状图实现钻取功能">js中,echar绘制柱状图实现钻取功能</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/javascript%e5%be%aa%e7%8e%af%e5%be%88%e9%87%8d%e8%a6%81%e5%90%97/" title="javascript循环很重要吗">javascript循环很重要吗</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/mysql%e5%b0%86%e5%a4%9a%e8%a1%8c%e5%90%88%e5%b9%b6%e5%a4%9a%e5%88%97/" title="mysql将多行合并多列">mysql将多行合并多列</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/layui%e7%9a%84%e4%b8%8b%e6%8b%89%e6%a1%86%e7%9a%84%e5%80%92%e4%b8%89%e8%a7%92/" title="layui的下拉框的倒三角">layui的下拉框的倒三角</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/rt-thread-%e4%bb%a5%e5%a4%aa%e7%bd%91%e5%92%8c4g%e5%88%87%e6%8d%a2/" title="RT-THREAD 以太网和4G切换">RT-THREAD 以太网和4G切换</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/phpstrom%e5%af%bc%e5%85%a5%e4%b8%80%e4%b8%aathinkphp%e9%a1%b9%e7%9b%ae/" title="phpstrom导入一个thinkphp项目">phpstrom导入一个thinkphp项目</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/%e5%90%af%e5%8a%a8%e9%a1%b9%e7%9b%ae%e6%8c%87%e5%ae%9astatic%e7%9a%84set%e6%96%b9%e6%b3%95/" title="启动项目指定static的set方法">启动项目指定static的set方法</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/bomblab%e6%89%80%e6%9c%89%e7%ad%94%e6%a1%88%e6%80%bb%e7%bb%93/" title="bomblab所有答案总结">bomblab所有答案总结</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/vmware16-%e6%97%a0%e6%b3%95%e5%ae%89%e8%a3%85%e7%b3%bb%e7%bb%9f/" title="vmware16 无法安装系统">vmware16 无法安装系统</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/vue3%e5%9c%b0%e5%9d%80%e6%a0%8f%e6%98%be%e7%a4%ba%e7%9a%84%e5%8f%82%e6%95%b0%e5%a6%82%e4%bd%95%e5%8f%96%e5%be%97/" title="vue3地址栏显示的参数如何取得">vue3地址栏显示的参数如何取得</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/android-threadpool%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86/" title="Android threadpool异常处理">Android threadpool异常处理</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/phaser-%e5%88%b6%e4%bd%9c%e7%bd%91%e6%b8%b8/" title="Phaser 制作网游">Phaser 制作网游</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/linux%e6%80%8e%e4%b9%88%e6%9f%a5%e5%9c%b0%e5%9d%80%e6%98%af%e5%90%a6%e5%86%b2%e7%aa%81/" title="linux怎么查地址是否冲突">linux怎么查地址是否冲突</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/setty-echo-%e9%80%80%e6%a0%bc%e9%94%ae%e5%87%ba%e7%8e%b0%e4%b9%b1%e7%a0%81/" title="setty echo 退格键出现乱码">setty echo 退格键出现乱码</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/%e4%b8%ba%e4%bb%80%e4%b9%88python%e7%94%a8open%e6%9c%89%e6%97%b6%e5%80%99%e4%b8%8d%e5%8a%a0encoding/" title="为什么Python用open有时候不加encoding">为什么Python用open有时候不加encoding</a> </div> </div> </div> </div> </div> </div> <div class="clearfix"></div> <div id="footer" class="two-s-footer"> <div class="footer-box"> <div class="container"> <div class="row" style="padding-left: 15px;padding-right: 15px;"> <div class="nav-footer"> <a href="/">Home</a> </div> <div class="copyright-footer"> <p>Powered By WordPress</p> </div> </div> </div> </div> </div> <div style="display:none"> </div> <script src="https://n4.ikafan.com/assets/js/echo.js"></script> <script> echo.init({ offset: 100, throttle: 250, unload: false, callback: function (element, op) { console.log(element, 'has been', op + 'ed') } }); var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f15ef43e2286a8db87bbc840d67d46b2"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>