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'))

 

转载于:https://www.cnblogs.com/wuheng-123/p/9682977.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值