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