zoukankan      html  css  js  c++  java
  • python全栈开发day112-CBV、flask_session、WTForms

        1.Flask 中的 CBV
            class Index(views.MethodView):
                # methods = ["POST"]
                # decorators = [war,nei]
    
                def get(self):
                    return "I am Gay"
    
                def post(self):
                    return "I am Les"
    
            app.add_url_rule("/index", endpoint="calss_index", view_func=Index.as_view(name="index"))
            as_view中的name 是在endpoint存在时作为 endpoint
        
        2.Flask中的插件 之 终于可以不用session  Flask-Session
            导入:
                from flask import Flask, session
                from flask_session import Session
                from redis import Redis
            配置:
                app.config["SESSION_TYPE"] = "redis"
                app.config["SESSION_REDIS"] = Redis(host="127.0.0.1",port=6379,db=5)
            替换:
                Session(app)
            使用:
                session["user"] = "aasdf"
                session.get("user")
        
        3.Flask中的插件 之 如果你不使用前后端分离的话,这东西很好用 WTForms
            from wtforms.fields import simple
            from wtforms import Form
            from wtforms import validators
    
            class LoginForm(Form):
                username = simple.StringField(
                    label="用户名",
                    validators=[
                        validators.DataRequired(message="数据不能为空"),
                        validators.Length(min=5, max=16, message="大于5小于16")
                    ],
                    render_kw={"class": "jinyinwangba"}
                )
                
            class Index(views.MethodView):
                def get(self):
                    loginfm = LoginForm()
                    return render_template("index.html", fm=loginfm)
                def post(self):
                    loginfm = LoginForm(request.form)
                    if not loginfm.validate():
                        return render_template("index.html", fm=loginfm)
                    session["user"] = "I am jinyinwangba"
                    return "I am Les"
            

     补充:

    1.{{ form.csrf_token }}最好改为{{ form.hidden_tag() }}。

    <form method="POST" action="/">
    {{ form.csrf_token }}
    {{ form.name.label }} {{ form.name(size=20) }}
    <input type="submit" value="Go">
    </form>

    https://www.cnblogs.com/haiyan123/p/8254228.html

  • 相关阅读:
    存储过程与事务实现转账
    win7创建虚拟无线网络
    .net制作安装包 如何生成快捷方式
    安装EFCodeFirst失败。。。。
    用VS2010自带的Library Package Manager安装EFCodeFirst出现“无法加载一个或多个请求的类型”错误的解决方法
    Java struts2
    Java spring
    Java web
    传输层
    xml
  • 原文地址:https://www.cnblogs.com/wuchenggong/p/9772511.html
Copyright © 2011-2022 走看看