1. 程式人生 > >flask 之cbv ,flash閃現,Flask_Session,WTForms - MoudelForm

flask 之cbv ,flash閃現,Flask_Session,WTForms - MoudelForm

1.CBV :
 from flask import views
 
 class LoginView(views.MethodView):
  def get(self):
   return
   
  def post(self):
   return
   
 
 app.add_url_rule("/login",endpoint=None,view_func=LoginView.as_view("login"))
 
2.flash:
 from flask import flash,get_flash_messages
 
 flash("","tag")
 get_flash_messages("tag")
  3.Flask-Session
 from flask_session import Session
 from flask import session
 
 app.config["SESSION_TYPE"] = "redis"
 app.config["SESSION_REDIS"] = Redis("127.0.0.1",6379,db=7)
 Session(app)
 
 session["user"] = "123"
 session.get("user")
 
4.WTForms - MoudelForm
 from wtfroms.fields import simple,core
 from wtfroms import Form,validators
 
 class LoginForm(Form):
  username = simple.StringFields(
   label = "" #看原始碼 __init__
  )
  
 
 lf = LoginForm()
 render_template("html",lf=lf)
 
 {{ lf.uername }}
 {{ lf.uername.label }}
 {{ lf.uername.errors.0 }}
 
 lf = LoginForm(request.form)
 
 if not lf.validata():
  render_template("html",lf=lf)
 
 lf.data.get("username")
    **************************************************************************************************************************************  
from flask import Flask,request,render_template
from flask import views
from wtforms import Form #類似 form表單
from wtforms.fields import simple,core
from wtforms import validators #驗證器

app=Flask(__name__)
#登入頁面認證
class LoginForm(Form):
username=simple.StringField(
label='使用者名稱',
validators=[
validators.DataRequired(message='不能為空'),#直接把錯誤寫入
validators.Length(min=3,max=5,message='不能小於3位,不能大於5位')
],
render_kw={'class':'my_username'}, #相等於自定義的類,屬性
)

password=simple.PasswordField(
label='密碼',
validators=[
validators.DataRequired(message='不能為空'),
validators.length(min=6,max=6,message='密碼必須為6位'),
validators.Regexp(regex='\d+',message='密碼必須為數字'),
],
render_kw={'class':'my_password'}
)

#註冊頁面認證
class RegForm(Form):
username=simple.StringField(
label='密碼',
validators=[
validators.DataRequired(message='不能為空'),
validators.Length(min=3,max=5,message='')
],
render_kw={'class':'my_username'}
)
nickname=simple.StringField(
label='暱稱',
validators=[
validators.DataRequired(message='不能為空')],
render_kw={'class':'my_nickname'}
)

password=simple.PasswordField(
label='密碼',
validators=[
validators.DataRequired(message='不能為空'),
validators.Length(min=6,max=6,message='密碼必須為6位'),
validators.Regexp(regex='\d+',message='密碼必須為數字')
],
render_kw={'class':'my_password'}
)

repassword=simple.PasswordField(
label='確認密碼',
validators=[ #EqualTo 等於 與欄位匹配
validators.EqualTo(fieldname='password',message='兩次不一致')
],
render_kw={'class':'my_password'}
)

email=simple.StringField(
label='郵箱',
validators=[
validators.Email(message='格式不正確')
],
render_kw={'class':'myemail'}
)

#上面用simple 下面用core
gender=core.RadioField(
label='性別',
coerce=int, #型別為int
choices=(
(1,'女'),
(2,'男')
),
default=1 #預設為1
)

hobby=core.SelectMultipleField(
label='愛好',
coerce=int,
choices=(
(1,'小姐姐'),
(2,'小蘿莉'),
(3,'小哥哥'),
(4,'正太'),
(5,'小阿姨'),
(6,'小大叔'),
),
default=(1,2,5)
)

submit=simple.SubmitField(
label='提交'
)



#登入驗證
class LoginClass(views.MethodView):
def get(self):
froms=LoginForm()
return render_template('login.html',froms=froms)

def post(self):
froms=LoginForm(request.form)
if froms.validate(): #django 是is_valid()
return '110'
else:
return render_template('login.html',froms=froms)

app.add_url_rule('/login',view_func=LoginClass.as_view('login'))


#註冊

class RegClass(views.MethodView):

def get(self):
froms=RegForm()
return render_template('reg.html',froms=froms)

def post(self):
froms=RegForm(request.form)
if froms.validate():
return '120'
else:
return render_template('reg.html',froms=froms)
app.add_url_rule('/reg',view_func=RegClass.as_view('reg'))




if __name__ == '__main__':
app.run(debug=True)