完成注册功能
Posted 杜丽晖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成注册功能相关的知识,希望对你有一定的参考价值。
- js文件: onclick函数return True时才提交表单,return False时不提交表单。
- html文件:
- <form>中设置 action和method="post"
- <input> 中设置 name
- 主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重定向到登录页
function myLogin() { var oUname = document.getElementById("name"); var oError = document.getElementById("error_box"); var opassword = document.getElementById("password"); var opassword1 = document.getElementById("password1"); 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="用户名首字母不能为数字"; return; } 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="用户名字符仅能为字母或数字"; isError = false; return isError; } } if(opassword.value.length<6||opassword.value.length>20){ oError.innerHTML="密码需在6-20个字符内"; isError = false; return isError; } if (opassword1.value != opassword.value) { oError.innerHTML = "您输入的密码不一致!请重新输入!"; isError = false; return isError; } window.alert("注册成功!"); return isError; }
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) telephone = db.Column(db.String(11), nullable=False) #db.create_all() # 增加数据 # user=User(username = ‘mis1234‘,password = ‘111111‘,telephone = ‘12345678900‘) # db.session.add(user) # db.session.commit() # 查询数据 # user=User.query.filter(user.username == ‘mis1234‘).first() # print(user.username,user.password) # 修改数据 # user=User.query.filter(user.username == ‘mis1234‘).first() # user.password = ‘000000‘ # db.session.commit() # 删除数据 # user=User.query.filter(user.username == ‘mis1234‘).first() # db.session.delete(user) # db.session.commit() @app.route(‘/login/‘) def login(): return render_template(‘login.html‘) @app.route(‘/register/‘,methods=[‘GET‘,‘POST‘]) def register(): if request.method == ‘GET‘: return render_template(‘register.html‘) else: usern = request.form.get(‘username‘) telep = request.form.get(‘telephone‘) passw = request.form.get(‘password‘) user = User.query.filter(User.username == usern).first() if user: return ‘username existed.‘ else: user1 = User(username = usern,password = passw,telephone = telep) db.session.add(user1)#数据库操作 db.session.commit() return redirect(url_for(‘login‘)) @app.route(‘/question‘,methods=[‘GET‘,‘POST‘]) def question(): return render_template(‘question.html‘) @app.route(‘/base‘,methods=[‘GET‘,‘POST‘]) def base(): return render_template(‘base.html‘) @app.route(‘/‘,methods=[‘GET‘,‘POST‘]) def index(): return render_template(‘index.html‘) if __name__ == ‘__main__‘: app.run(debug=True)
以上是关于完成注册功能的主要内容,如果未能解决你的问题,请参考以下文章