zoukankan      html  css  js  c++  java
  • flask standrad class 使用

    from flask import Flask,views,url_for
    
    
    app = Flask(__name__)
    
    
    class IndexView(views.View):
        def dispatch_request(self): #必须实现
    print(url_for('index'))
            return 'hello world'
    
    app.add_url_rule('/',endpoint='index',view_func=IndexView.as_view('index'))
    
    
    if __name__ == '__main__':
        app.run(debug=True)

     #上面的类方法看起来并没有函数好用,那为什么还要用它。

    #比如说有几个直传json数据的api
    class JSONView(views.View):
    
        def get_data(self):
            raise NotImplementedError
    
        def dispatch_request(self):
            return jsonify(self.get_data())
    
    
    class ListView(JSONView):
        def get_data(self):
            return {"username":"xiaowu","password":"123456"}
    
    app.add_url_rule('/list/',endpoint='list',view_func=ListView.as_view('list'))
    #几个api要传递统一的变量
    class UNIFYView(views.View):
        def __init__(self,*args,**kwargs):
            super().__init__(*args,**kwargs)
            self.content = {"abs":"今年过年不收礼,收礼只收女票"}
    
    
    class LoginView(UNIFYView):
        def dispatch_request(self):
            self.content.update(
                username = "xiaowu",
                password = "123456"
            )
            return render_template('login.html',**self.content)
    
    app.add_url_rule('/login/',endpoint='login',view_func=LoginView.as_view('login'))
    
    
    class RegisterView(UNIFYView):
        def dispatch_request(self):
            return render_template('login.html', **self.content)
    
    
    app.add_url_rule('/register/', endpoint='register', view_func=LoginView.as_view('register'))
  • 相关阅读:
    py基础之模块与包
    py装饰器,生成器,迭代器
    py函数式编程
    py基础之列表生成式
    算法基础之递归算法
    Py基础之函数
    py基础之无序列表
    py基础之数据类型及基本语法
    jsp报错问题汇总
    mysql问题汇总
  • 原文地址:https://www.cnblogs.com/wuheng-123/p/9682977.html
Copyright © 2011-2022 走看看