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'))
  • 相关阅读:
    Linux 设备驱动 Edition 3(中文版)
    内核和用户空间共享内存的实现例程proc和mmap
    mmap的详细使用(用户空间)
    使用 I/O 内存from LDD3
    关于strcpy、memset、memcpy的使用详解
    get_free_page 和其友
    Linux调试技术介绍
    关于信息时代的学习
    [恢]hdu 1196
    [恢]hdu 1157
  • 原文地址:https://www.cnblogs.com/wuheng-123/p/9682977.html
Copyright © 2011-2022 走看看