zoukankan      html  css  js  c++  java
  • Flask初级(九)flash与前台交互get详解

    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)

     

    访问 则使用视图函数hello,并将name设一个默认值jack

    访问 /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冒号后面是命名这个变量,给下面的函数交互输入。

    可匹配

    /

    /***

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

  • 相关阅读:
    【原理】【重点】异步回调的一些实现策略
    上下文传递
    洋码头全异步服务框架
    秒杀系统架构优化思路
    从urllib2的内存泄露看python的GC python引用计数 对象的引用数 循环引用
    jvisualvm All-in-One Java Troubleshooting Tool
    小心踩雷,一次Java内存泄漏排查实战
    django 请求处理流程 链路追踪
    存储过程
    Dijkstra's algorithm
  • 原文地址:https://www.cnblogs.com/jackadam/p/8144989.html
Copyright © 2011-2022 走看看