完成注册功能
Posted 塨槟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成注册功能相关的知识,希望对你有一定的参考价值。
1、js文件: onclick函数return True时才提交表单,return False时不提交表单。
1 function fnLogin() { 2 var oUname = document.getElementById("uname"); 3 var oError = document.getElementById("error_box"); 4 var oUpass = document.getElementById("upass"); 5 var oUpass1 = document.getElementById("upass1"); 6 var isError=true; 7 oError.innerhtml="<br>"; 8 9 if(oUname.value.length<6 || oUname.value.length >20){ 10 oError.innerHTML="用户名6-20"; 11 isError=false; 12 return isError; 13 }else if ((oUname.value.charCodeAt(0)>=48) && (oUname.value.charCodeAt(0)<=57)){ 14 oError.innerHTML="第一位只能是字母"; 15 isError=false; 16 return isError; 17 } else { 18 for (var i=0 ; i<oUname.value.length;i++){ 19 if (oUname.value.charCodeAt(i)<48 || (oUname.value.charCodeAt(i)>57)&&(oUname.value.charCodeAt(i)<97)|| oUname.value.charCodeAt(i)>122){ 20 oError.innerHTML="只能是字母或数字"; 21 isNotError=false; 22 return isError; 23 } 24 } 25 } 26 if(oUpass.value.length<6 || oUpass.value.length>12){ 27 oError.innerHTML="密码6-12位"; 28 isError=false; 29 return isError; 30 } 31 if (oUpass1.value!=oUpass.value ) { 32 oError.innerHTML = "两次输入密码不一致!"; 33 isError=false; 34 return isError; 35 } 36 return isError; 37 window.alert("注册成功") 38 }
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’])
def regist():
if request.method == ‘GET‘:
return render_template(‘regist.html‘)
else:
username = request.form.get(‘username’)#获取form中的数据
判断用户名是否存在
存到数据库中
redirect重定向到登录页
from flask import Flask,render_template,request,redirect,url_for from flask_sqlalchemy import SQLAlchemy import config app = 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 = ‘nini‘,password = ‘11122‘) # db.session.add(user) # db.session.commit() # # #查询 # user=User.query.filter(User.username ==‘nini‘).first() # print(user.username,user.password) # # #删除 # user=User.query.filter(User.username ==‘nini‘).first() # db.session.delete(user) # db.session.commit() # # #修改 # user=User.query.filter(User.username ==‘nini‘).first() # user.password=‘11122333‘ # db.session.commit() #db.create_all() #@app.route(‘/‘) #def hello_world(): #return ‘Hello World!‘ @app.route(‘/‘) def index(): return render_template(‘index.html‘) @app.route(‘/register/‘,methods=[‘GET‘,‘POST‘]) def register(): if request.method == ‘GET‘: return render_template(‘register.html‘) else: username=request.form.get(‘username‘) 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(‘login.html‘) # # @app.route(‘/shuoshuo/‘) # def shuoshuo(): # return render_template(‘shuoshuo.html‘) if __name__==‘__main__‘: app.run(debug=True)
以上是关于完成注册功能的主要内容,如果未能解决你的问题,请参考以下文章