完成注册功能
- js文件: onclick函数return True时才提交表单,return False时不提交表单。
function fnLogin() { var oUname = document.getElementById("uname"); var oError = document.getElementById("error_box"); var oUpass = document.getElementById("upass"); var oUpass1 = document.getElementById("upass1"); var isError=true; oError.innerHTML="<br>"; if(oUname.value.length<6 || oUname.value.length >20){ oError.innerHTML="用户名6-20"; isError=false; return isError; }else if ((oUname.value.charCodeAt(0)>=48) && (oUname.value.charCodeAt(0)<=57)){ oError.innerHTML="第一位只能是字母"; isError=false; return isError; } else { for (var i=0 ; i<oUname.value.length;i++){ if (oUname.value.charCodeAt(i)<48 || (oUname.value.charCodeAt(i)>57)&&(oUname.value.charCodeAt(i)<97)|| oUname.value.charCodeAt(i)>122){ oError.innerHTML="只能是字母或数字"; isNotError=false; return isError; } } } if(oUpass.value.length<6 || oUpass.value.length>12){ oError.innerHTML="密码6-12位"; isError=false; return isError; } if (oUpass1.value!=oUpass.value ) { oError.innerHTML = "两次输入密码不一致!"; isError=false; return isError; } return isError; window.alert("注册成功") }
- html文件:
- <form>中设置 action和method="post"
- <input> 中设置 name
<div class="box"> <h2>REGISTER</h2> <form action="{{url_for('register') }}" method="post"> <div class="input_box"> 用户名:<input id="uname" type="text" placeholder="请输入用户名" name="'username"><br> </div> <div class="input_box"> 昵称:<input id="nickname" type="text" placeholder="请输入昵称" name="'nickname"><br> </div> <div class="input_box"> 密码:<input id="upass" type="password" placeholder="请输入密码(区分大小写)" name="'password"><br> </div> <div class="input_box"> 验证:<input id="upass1" type="password" placeholder="再输入密码(区分大小写)"> </div> <div id="error_box"> </div><br> <div class="input_box"> <button onclick="fnLogin()" >register</button> </div> </form> </div>
- 主py文件中:
- from flask import request, redirect, url_for
- @app.route('/regist/', methods=['GET', 'POST’])
from flask import Flask,render_template,request,redirect,url_for from flask_sqlalchemy import SQLAlchemy import config app = Flask(__name__)#初始化一个Flask对象,需要传递一个参数__name__ app.config.from_object(config) db=SQLAlchemy(app) # /创建用户模型 class User(db.Model): __tablename__ ='user' id=db.Column(db.Integer,primary_key=True,autoincrement=True) username=db.Column(db.String(20),nullable=False) password=db.Column(db.String(20),nullable=False) nickname=db.Column(db.String(50)) #db.create_all() # #增加 # user =User(username = 'yuan1',password = 'yuan') # db.session.add(user) # db.session.commit() # # #查询 # user=User.query.filter(User.username =='yuan1').first() # print(user.username,user.password) # # #删除 # user=User.query.filter(User.username =='yuan1').first() # db.session.delete(user) # db.session.commit() # # #修改 # user=User.query.filter(User.username =='yuan1').first() # user.password='yuan11111' # db.session.commit() #db.create_all() #@app.route('/') #这是一个装饰器,在函数上面,其作用是做一个URL与视图函数的映射, #http://127.0.0.1:5000/ 去执行hello world()这个函数 #def hello_world(): #return 'Hello World!' @app.route('/') def index(): return render_template('1101.html') @app.route('/shouye/') def shouye(): return render_template('shouye.html') @app.route('/register/',methods=['GET','POST']) def register(): if request.method == 'GET': return render_template('31.html') else: username=request.form.get('username') #获取form中的数据 password =request.form.get('password') nickname=request.form.get('nickname') user =User.query.filter(User.username==username).first()#判断用户名是否存在 if user: return 'username existed' else: user=User(username=username,password=password,nickname=nickname) db.session.add(user)#数据库操作 db.session.commit()#存到数据库中 return redirect(url_for('login'))#重定向到登录页 @app.route('/login/',methods=['GET','POST']) def login(): return render_template('1031.html') @app.route('/shuoshuo/') def shuoshuo(): return render_template('shuoshuo.html') if __name__=='__main__':#当前这个文件作为主程序运行,就会执行这段,作为模板 app.run(debug=True) #启动一个web服务器,来监听并接受用户的请求
def regist():
if request.method == 'GET':
return render_template('regist.html')
else:
username = request.form.get(‘username’)#获取form中的数据
判断用户名是否存在
存到数据库中
redirect重定向到登录页