11.1.前臺用戶模型創建 (1)apps/front/models.py 首先安裝:pip install shortuuid (2)manage.py (3)生成表和添加前臺用戶 添加用戶 11.2.註冊界面完成 (1)front/views.py (2)common/images/logo.j ...
11.1.前臺用戶模型創建
(1)apps/front/models.py
首先安裝:pip install shortuuid
class FrontUser(db.Model): __tablename__ = "front_user" id = db.Column(db.String(100),primary_key=True,default=shortuuid.uuid) telephone = db.Column(db.String(11),nullable=False,unique=True) username = db.Column(db.String(50),nullable=False) _password = db.Column(db.String(100),nullable=False) email = db.Column(db.String(50),unique=True) realname = db.Column(db.String(50)) avatar = db.Column(db.String(100)) signature = db.Column(db.String(100)) gender = db.Column(db.Enum(GenderEnum),default=GenderEnum.UNKNOW) join_time = db.Column(db.DateTime,default=datetime.now) def __init__(self,*args,**kwargs): #如果傳入的參數裡面有‘password’,就單獨處理 if "password" in kwargs: self.password = kwargs.get("password") #處理完後把password pop出去 kwargs.pop("password") #剩下的參數交給父類去處理 super(FrontUser, self).__init__(*args,**kwargs) @property def password(self): return self._password #保存密碼的時候加密 @password.setter def password(self, raw_password): self._password = generate_password_hash(raw_password) def check_password(self, raw_password): result = check_password_hash(self.password, raw_password) return result
(2)manage.py
@manager.option('-t','--telephone',dest='telephone') @manager.option('-u','--username',dest='username') @manager.option('-p','--password',dest='password') def create_front_user(telephone,username,password): user = FrontUser(telephone=telephone,username=username,password=password) db.session.add(user) db.session.commit()
(3)生成表和添加前臺用戶
python manage.py db migrate
python manage.py db upgrade
添加用戶
python manage.py create_front_user -t 18888888888 -u huge -p 111111
11.2.註冊界面完成
(1)front/views.py
class SignupView(views.MethodView): def get(self): return render_template('front/signup.html') bp.add_url_rule('/signup/',view_func=SignupView.as_view('signup'))
(2)common/images/logo.jpg
放一張logo圖片
(3)front/front_signup.html
{% from 'common/_macros.html' import static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>仙劍賬號註冊</title> <script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script> <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <script src="http://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <style> body{ background:#f3f3f3; } .outer-box{ width: 854px; background: #fff; margin: 0 auto; overflow: hidden; } .logo-box{ text-align: center; padding-top: 40px; } .logo-box img{ width: 60px; height: 60px; } .page-title{ text-align: center; } .sign-box{ width: 300px; margin: 0 auto; padding-top: 50px; } </style> </head> <body> <div class="outer-box"> <div class="logo-box"> <a href="/"> <img src="{{ static('common/images/logo.jpg') }}" alt=""> </a> </div> <h2 class="page-title">仙劍賬號註冊</h2> <div class="sign-box"> <div class="form-group"> <div class="input-group"> <input type="text" class="form-control" name="telephone" placeholder="手機號碼"> <span class="input-group-btn"> <button class="btn btn-default">發送驗證碼</button> </span> </div> </div> <div class="form-group"> <input type="text" class="form-control" name="sms_captcha" placeholder="簡訊驗證碼"> </div> <div class="form-group"> <input type="text" class="form-control" name="username" placeholder="用戶名"> </div> <div class="form-group"> <input type="password" class="form-control" name="password1" placeholder="密碼"> </div> <div class="form-group"> <input type="password" class="form-control" name="password2" placeholder="確認密碼"> </div> <div class="form-group"> <div class="input-group"> <input type="text" class="form-control" name="graph_captcha" placeholder="圖形驗證碼"> <span class="input-group-addon"> 驗證碼 </span> </div> </div> <div class="form-group"> <button class="btn btn-warning btn-block">立即註冊</button> </div> </div> </div> </body> </html>
效果: