使用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/A2dm6n77ze/">【Leetcode_easy】1122. Relative Sort Array</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">problem 1122. Relative Sort Array 参考 1. Leetcode_easy_1122. Relative Sort Array; 2. helloacm; 完</p> </li> <li><a href="https://www.shuzhiduo.com/A/Ae5RnX3A5Q/">react中如何实现一个按钮的动态隐藏和显示(有效和失效)</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">初始准备工作 constructor(props) { super(props); /* * 构建导出数据的初始参数,结合用户下拉选择后动态设置参数值 * */ this.state = { btnS ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/MyJx7gA1zn/">iOS使用UIImageView展现网络图片(转载)</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">在iOS开发过程中,经常会遇到使用UIImageView展现来自网络的图片的情况,最简单的做法如下: [cpp] view plaincopy   - (void)viewDidLoad { [sup ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/l1dyomN0Je/">记录一下我的git连接不上GitHub问题</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">1.日常操作,提交代码,报错误下: $ git clone git@github.com:hanchao5272/myreflect.git Cloning into 'myreflect'... s ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/MyJx7gDezn/">leetcode1186 Maximum Subarray Sum with One Deletion</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">思路: 最大子段和的变体,前后两个方向分别扫一遍即可. 实现: class Solution { public: int maximumSum(vector<int>& arr) ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/D854DoDW5E/">为何我的网站http总是跳转https?能不能让http不跳转https?谈谈我遇到的坑和解决方案。</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">如题,突然想给我的个人网站加一个ssl,让它能够通过https访问. 但一顿操作猛如虎后,发现只能https访问了,手动输入http也会无限跳转到https. 现在说下我的排查思路和解决方案: 1. ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/rV57WLoV5P/">SQL查询当天、本周、本月记录详解</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">--查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: select * from info wh ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/gVdnn4aDdW/">word模板文档填充数据</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">1.切记一定要用Word 97-2003(*.doc)格式的文档另存为 *.xml格式的文件,而不能用 Word 2007(*.docx)格式的word文档转 *.xml,将转成功之后的xml文件放入 ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/Ae5Rn13N5Q/">Extjs editor 设置默认值</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">一.前言 Ext js 给 editor 设置默认值用 value 无效,在 Model 中添加 defaultValue 即可. 二.实例 view: Ext.define('xxxx.view.P ...</p> </li> <li><a href="https://www.shuzhiduo.com/A/kmzL2PYl5G/">给内部类对象数组属性赋值时报错:Exception in thread "main" java.lang.NullPointerException</a> <p style="color: rgba(0, 0, 0, 0.32);margin-bottom: 8px;">前言 1255: 打怪升级(Java),写这个题目程序的时候,控制台提示如下错误: Exception in thread "main" java.lang.NullPointer ...</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/anaconda-navigator-home%e7%95%8c%e9%9d%a2%e7%a9%ba%e7%99%bd/" title="anaconda navigator home界面空白">anaconda navigator home界面空白</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/%e5%9f%ba%e4%ba%8etoken%e7%9a%84%e4%bc%9a%e8%af%9d/" title="基于token的会话">基于token的会话</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/%e5%ae%89%e8%a3%85ros-guoneiyuan/" title="安装ros guoneiyuan">安装ros guoneiyuan</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f-%e6%80%8e%e6%a0%b7%e5%88%a4%e6%96%advideo%e5%9c%a8%e5%8a%a0%e8%bd%bd%e4%b8%ad/" title="微信小程序 怎样判断video在加载中">微信小程序 怎样判断video在加载中</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/easyui-dialog-%e6%8b%96%e5%8a%a8/" title="easyui-dialog 拖动">easyui-dialog 拖动</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/c-webbrowser1-%e8%8e%b7%e5%8f%96html%e4%bb%a3%e7%a0%81/" title="c# webBrowser1 获取html代码">c# webBrowser1 获取html代码</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/window-open%e6%96%b0%e7%aa%97%e5%8f%a3/" title="window.open新窗口">window.open新窗口</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/loaderrunner-%e6%94%af%e6%8c%81websocket%e5%90%97/" title="loaderrunner 支持websocket吗">loaderrunner 支持websocket吗</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/dv-scroll-board-%e8%a1%a8%e6%a0%bc%e6%98%be%e7%a4%ba%e7%9c%81%e7%95%a5%e5%8f%b7/" title="dv-scroll-board 表格显示省略号">dv-scroll-board 表格显示省略号</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/centos7%e4%b8%8b%e8%bd%bdrpm%e5%8c%85/" title="centos7下载rpm包">centos7下载rpm包</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/sap%e6%8a%a5%e5%b7%a5%e6%98%be%e7%a4%ba%e7%bc%ba%e5%b0%91%e5%ad%98%e5%82%a8%e5%8d%95%e5%85%83%e7%9a%84%e8%ae%a2%e5%8d%95%e7%a1%ae%e8%ae%a4%e5%8f%b7/" title="Sap报工显示缺少存储单元的订单确认号">Sap报工显示缺少存储单元的订单确认号</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/%e6%89%b9%e9%87%8f%e5%81%9c%e6%ad%a2docker%e5%ae%b9%e5%99%a8/" title="批量停止docker容器">批量停止docker容器</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/etc-init-d%e7%9b%ae%e5%bd%95%e4%b8%8b%e6%b2%a1%e6%9c%89network-manager/" title="etc init.d目录下没有network-manager">etc init.d目录下没有network-manager</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/java-%e8%80%97%e6%97%b6%e6%93%8d%e4%bd%9c-%e5%bc%82%e6%ad%a5%e6%89%a7%e8%a1%8c/" title="java 耗时操作 异步执行">java 耗时操作 异步执行</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/thinkphp-%e9%85%8d%e7%bd%aephpstorm/" title="ThinkPHP 配置phpstorm">ThinkPHP 配置phpstorm</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/pta%e5%88%97%e8%a1%a8%e4%b8%8e%e5%85%83%e7%bb%84-%e5%ba%8f%e5%88%97-%e4%bd%9c%e4%b8%9a/" title="PTA列表与元组(序列)作业">PTA列表与元组(序列)作业</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/%e6%80%8e%e4%b9%88%e5%9c%a8service%e5%b1%82%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6%e6%b5%81/" title="怎么在service层下载文件流">怎么在service层下载文件流</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/%e6%96%87%e4%bb%b6%e6%b5%81%e8%be%93%e5%87%ba%e5%88%b0%e6%8c%87%e5%ae%9a%e6%96%87%e4%bb%b6%e5%a4%b9/" title="文件流输出到指定文件夹">文件流输出到指定文件夹</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/java%e8%be%93%e5%87%ba%e6%97%a5%e5%bf%97%e5%88%b0%e6%96%87%e4%bb%b6/" title="java输出日志到文件">java输出日志到文件</a> </div> <div class="tag-items" style="float: left;"> <a href="https://www.shuzhiduo.com/topic/%e9%98%bb%e6%ad%a2%e4%bf%9d%e5%ad%98%e8%a6%81%e6%b1%82%e9%87%8d%e6%96%b0%e5%88%9b%e5%bb%ba%e8%a1%a8%e7%9a%84%e6%9b%b4%e6%94%b9/" title="阻止保存要求重新创建表的更改">阻止保存要求重新创建表的更改</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>