1. 程式人生 > >完成註冊功能

完成註冊功能

del 字符 var box main tel from rect login

  1. js文件: onclick函數return True時才提交表單,return False時不提交表單。
 1 function fnLogin() {
 2     var oUname = document.getElementById("uname")
 3     var oUpass = document.getElementById("upass")
 4      var oUpass2=document.getElementById("upass2");
 5     var oError = document.getElementById("error_box"
) 6 7 oError.innerHTML="<br>" 8 if (oUname.value.length > 20 || oUname.value.length < 6) { 9 oError.innerHTML = "請輸入用戶名6-20位字符" 10 isError =false; 11 return isError; 12 }else if ((oUname.value.charCodeAt(0)>=48)&&(oUname.value.charCodeAt(0)<=57)){
13 oError.innerHTML="用戶名首字母必須是字母" 14 isError =false; 15 return isError; 16 }else for(var i=0; i<oUname.value.length;i++){ 17 if((oUname.value.charCodeAt(i)<48)||(oUname.value.charCodeAt(i)>57)&&(oUname.value.charCodeAt(i)<97)||(oUname.value.charCodeAt(i)>122)){
18 oError.innerHTML="用戶名必須為字母或數字"; 19 isError =false; 20 return isError; 21 } 22 23 } 24 25 26 27 28 if (oUpass.value.length > 20 || oUpass.value.length < 6) { 29 oError.innerHTML = "請輸入密碼 6-20位字符" 30 isError =false; 31 return isError; 32 } 33 else if (oUpass.value!==oUpass2.value){ 34 oError.innerHTML="兩次密碼不一致"; 35 isError =false; 36 return isError; 37 } 38 return isError; 39 window.alert("註冊成功") 40 41 }

  1. html文件:
    1. <form>中設置 action和method="post"
    2. <input> 中設置 name
    3.  1 <div class="box">
       2     <h2>REGISTER</h2>
       3      <form action="{{url_for(‘register‘) }}" method="post">
       4          <div class="input_box">
       5              用戶名:<input id="uname" type="text" placeholder="請輸入用戶名" name="‘username"><br>
       6          </div>
       7          <div class="input_box">
       8               昵稱:<input id="nickname" type="text" placeholder="請輸入昵稱" name="‘nickname"><br>
       9          </div>
      10          <div class="input_box">
      11              密碼:<input id="upass" type="password" placeholder="請輸入密碼" name="‘password"><br>
      12          </div>
      13          <div class="input_box">
      14              驗證:<input id="upass2" type="password" placeholder="再輸入密碼">
      15          </div>
      16         <div id="error_box"> </div><br>
      17 
      18          <div class="input_box">
      19             <button onclick="fnLogin()" >register</button>
      20          </div>
      21      </form>
      22 </div>

  2. 主py文件中:
    1. from flask import request, redirect, url_for
    2. @app.route(‘/regist/‘, methods=[‘GET‘, ‘POST’])

def regist():

if request.method == ‘GET‘:

return render_template(‘regist.html‘)

else:

username = request.form.get(‘username’)#獲取form中的數據

判斷用戶名是否存在

存到數據庫中

redirect重定向到登錄頁

 1 from flask import Flask, request,redirect,url_for
 2 from flask import render_template
 3 from flask_sqlalchemy import SQLAlchemy
 4 import config
 5 
 6 app=Flask(__name__)
 7 app.config.from_object(config)
 8 db=SQLAlchemy(app)
 9 
10 class User(db.Model):
11     __tablename__=user
12     id=db.Column(db.Integer,primary_key=TabError,autoincrement=True)
13     username=db.Column(db.String(20),nullable=False)
14     password=db.Column(db.String(20),nullable=False)
15     nickname=db.Column(db.String(50))
16 
17 ‘‘‘
18 db.create_all()
19 
20 
21 # 數據添加
22 user=User(username=‘mis1555‘,password=‘mis1555‘)
23 db.session.add(user)
24 db.session.commit()
25 
26 # 數據更改
27 user = User.query.filter(User.username==‘mis1555‘).first()
28 user.password=‘111111‘
29 db.session.commit()
30 
31 # 數據刪除
32 user = User.query.filter(User.username==‘mis1555‘).first()
33 db.session.delete(user)
34 db.session.commit()
35 
36 # 數據查詢
37 user = User.query.filter(User.username==‘mis1555‘).first()
38 print(user.username,user.password)
39 ‘‘‘
40 
41 app = Flask(__name__)
42 @app.route(/)
43 def switch():
44     return render_template(moban.html)
45 @app.route(/login/)
46 def login():
47     return render_template(login.html)
48 @app.route(/register/,methods=[GET,POST])
49 def register():
50     if request.method==GET:
51         return render_template(register.html)
52     else:
53         username=request.form.get(username)
54         password = request.form.get(password)
55         nickname = request.form.get(nickname)
56         user=User.query.filter(User.username==username).first()
57         if user:
58             return uusername existed
59         else:
60             user=User(username=username,password=password,nickname=nickname)
61             db.session.add(user)
62             db.session.commit()
63             return  redirect(url_for(login.html))
64 
65 if __name__ == __main__:
66     app.run(debug=True)
67 if __name__ == __main__:
68       app.run()

完成註冊功能