Project name :Flask_Plan

templates:templates

static:static

@app.route('/')
def hello_world():
return 'index default'

直接get访问域名       http://127.0.0.1:5000

@app.route('/view_plan/')
def view_plan():
return render_template('plan.html') # 此行修改

get访问子目录  http://127.0.0.1:5000/view_plan

@app.route('/view_date/<date>')  # 增加<date>
def view_date(date): # 把date传入显示函数
print(date)
return render_template('plan.html')

get访问子目录,带参数   http://127.0.0.1:5000/view_date/20171212

后台会打印20171212

曾经让我冥思苦想2天的一个问题是:

上面3个路由和视图函数都写好了

http://127.0.0.1:5000/view_date/20171212

http://127.0.0.1:5000/view_plan

可以正常访问

http://127.0.0.1:5000/view_plan/20121212

http://127.0.0.1:5000/view_date/

直接404错误

如何使404错误的可以正常访问呢?

使用request。

from flask import Flask, render_template ,request

首先引入request

@app.route('/view_plan/')
def view_plan():
date = request.args.get('date')
print(date)
return render_template('plan.html') # 此行修改 # 历史计划的查询,输入日期,即可显示历史日期的计划
@app.route('/view_date/') # 增加<date>
def view_date(): # 把date传入显示函数
date = request.args.get('date')
print(date)
return render_template('plan.html') # 此行修改

然后修改视图函数。

访问的时候这样:

http://127.0.0.1:5000/view_plan/

http://127.0.0.1:5000/view_date/

都是正常访问。

http://127.0.0.1:5000/view_plan/?date=20171212

http://127.0.0.1:5000/view_date/?date=20171212

也可以正常访问了。多个参数可以用&连起来

http://127.0.0.1:5000/view_date/?date=20171212&carriage=552828

后台:

request.args.get(date)
request.args.get(carriage)

这样的地址看起来就像是动态地址。

想使用静态地址方式的方法有以下两种:

一:建立两条路由,视图函数引用。

@app.route('/hello/')
def hello(name='jack'):
return 'hello %s'%name @app.route('/hello/<name>')
def helloname(name):
return hello(name)

访问 http://domain.com/hello 则使用视图函数hello,并将name设一个默认值jack

访问 http://domain.com/hello/rose  则使用视图函数 helloname,实际是使用hello函数

一:使用路由的default参数:

@app.route('/', defaults={'name': 'jack'})
@app.route('/<name>')
def hello_world(name):
return 'hello %s'%name

两个路由装饰器,

看着应该是使用  ‘/<name>’,如果name没有提供,那么就使用默认值:jack

二:使flask路由解析支持正则表达式。

另一种解法:给flask的url路由增加正则功能。

from flask import Flask, render_template ,request
from werkzeug.routing import BaseConverter #此行新增
app = Flask(__name__)
class RegexConverter(BaseConverter): #此函数新增
def __init__(self, url_map, *items):
super(RegexConverter, self).__init__(url_map)
self.regex = items[0] app.url_map.converters['regex'] = RegexConverter #此对象新增 @app.route('/')
def hello_world():
return render_template('plan.html',)
@app.route('/hello/<regex(".*"):name>') #关键的路由正则匹配
def hello(name): #对应的视图函数,随便你怎么写吧。
if name =='':
return 'hello no name'
else:
return "hello '%s'" % (name)

引用BaseConverter,创建类 RegexConverter

实例化RegexConverter

在路由中使用正则规则/hello/<regex(".*"):name>

其中/hello/是固定匹配的前缀,就和匹配/hello/一样,<regex(""):>应该是规范格式,“.*”双引号里面是正则表达式,name冒号后面是命名这个变量,给下面的函数交互输入。

可匹配

http://domain.com/hello

http://domain.com/hello/

http://domain.com/hello/***

并把***传入视图函数。

Flask初级(九)flash与前台交互get详解的更多相关文章

  1. Flask初级(十)flash与前台交互post详解

    Project name :Flask_Plan templates:templates static:static POST提交方式,首先要有表单 老实去改模板文件吧. 查询窗口我准备放在页面最顶上 ...

  2. “全栈2019”Java第五十九章:抽象类与抽象方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  3. Flash播放控件属性详解

    Flash 播放控件属性详解 一.属性篇 1.AlignMode(读写)  语法:AlignMode As Long  说明:对齐方式(与SAlign 属性联动).当控件的长宽比例与影片不一致且WMo ...

  4. Kotlin——初级篇(四):控制语句详解

    在前面 的章节中讲解了Kotlin语言中的数据类型.变量与常量的定义.不了解请参见前面的内容: Kotlin--初级篇(三):数据类型详解. Kotlin--初级篇(二)常量.变量.注释. 下面详细为 ...

  5. Cocos 2d-X Lua 游戏添加苹果内购(二) OC和Lua交互代码详解

    这是第二篇 Cocos 2d-X Lua 游戏添加苹果内购(一) 图文详解准备流程 这是前面的第一篇,详细的说明了怎样添加内购项目以及填写银行信息提交以及沙盒测试员的添加使用以及需要我们注意的东西,结 ...

  6. Flask初级(八)flash与前台交互get post 简介

    Project name :Flask_Plan templates:templates static:static 两种 HTTP 请求方法:GET 和 POST在客户机和服务器之间进行请求-响应时 ...

  7. JSP数据交互——九大内置对象及其方法详解(一)

    ①既然说到JSP内置对象,那么什么是JSP内置对象呢? 解析:JSP内置对象,就是在编写JSP页面时,不需要做任何声明就可以直接使用的对象. 如下代码片段:  <% int[]  value  ...

  8. SuperSocket入门(三)-Telnet多服务实例和服务实例交互配置详解

        在SuperSocket入门(二)中我们已经简单了解了通过配置App.config文件使用BootStrap启动SuperSocket服务.我们先来看一下上个案例中的基本配置文件示例: < ...

  9. Java数据持久层框架 MyBatis之API学习九(SQL语句构建器详解)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

随机推荐

  1. windows的gvim总是报错: +iconv fencview.vim

    iconv是用来转换gvim文件的编码的, 需要插件: iconv.dll gvim7.3的文件目录结构: vim/vim73是它的核心文件, 而vimfiles是扩展文件, 里面的plugin是专门 ...

  2. Git 同时与多个远程库互相同步

    情形:有两个git服务器,比如github,gitosc,有个项目同时在两个服务器上,要互相同步 其实命令还是比较简单的,比如一个现有的git项目,在github,gitosc中分别创建好对应的项目. ...

  3. 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试

    相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控 用python + hadoop streami ...

  4. js键盘按钮keyCode及示例大全

    以功能区分布 以 keycode 编号顺序分布 keycode 0 = keycode 1 = keycode 2 = keycode 3 = keycode 4 = keycode 5 = keyc ...

  5. python 集合的比较

    setx = set(["apple", "mango"]) sety = set(["mango", "orange" ...

  6. Jmeter ResponseAssertion 【Ignore Status】

    在Jmeter源码中AssertionGui.java中,定义了Ignore Status的作用域 /** * Checkbox to indicate whether the response sh ...

  7. STL_算法_03_拷贝和替换算法

    ◆ 常用的拷贝和替换算法: 1.1.复制(容器A(全部/部分) 复制到 容器B(全部/部分)),返回的值==>iteratorOutBegin.end() iterator copy(itera ...

  8. node 循序渐进

    1. 执行 node helloworld.js 2. http  服务器 建 server.js 文件 -  node server.js  跑起来 -  浏览器访问  http://localho ...

  9. JavaScript权威指南--事件处理

    知识要点 客户端JavaScript程序采用了异步事件驱动变成模型(13.3.2节).这种风格并不只应用于web编程,所有使用图形用户界面的应用程序都采用它,它们静待某些事件发生,然后响应. 事件就是 ...

  10. UVA - 11853 Paintball(dfs)

    UVA - 11853 思路:dfs,从最上面超过上边界的圆开始搜索,看能不能搜到最下面超过下边界的圆. 代码: #include<bits/stdc++.h> using namespa ...