1.js文件: onclick函数return True时才提交表单,return False时不提交表单。
2.html文件:
- <form>中设置 action和method="post"
- <input> 中设置 name
3.主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) db.create_all() @app.route('/') def base(): return render_template('base.html') @app.route('/login/',methods=['GET','POST']) def login(): return render_template('login.html') @app.route('/regist/',methods=['GET','POST']) def regist(): if request.method == 'GET': return render_template("regist.html") else : username = request.form.get('username') password = request.form.get('password') user = User.query.filter(User.username == username).first() if user: return 'Username existed' else: user1 = User(username=username, password=password) db.session.add(user1) db.session.commit() return redirect(url_for('login')) @app.route('/question/') def question(): return render_template('question.html') if __name__ == '__main__': app.run(debug=True)
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:@localhost:3306/register?charset=utf8' SQLALCHEMY_TRACK_MODIFICATIONS = False
function MyRegister() { var oUname=document.getElementById("uname"); var oFpass=document.getElementById("fpass"); var oSpass=document.getElementById("spass"); var oError=document.getElementById("errorbox"); //username if(oUname.value=="Username"&&oFUpass.value=="Password"&&oSUpass.value=="Password"){ oError.innerHTML="Please input username"; return false; }else if(oUname.value.length<6||oUname.value.length>12){ oError.innerHTML="Username must be 6 to 12"; return false; }else if(oUname.value.charCodeAt(0)>=48 && oUname.value.charCodeAt(0)<=57){ oError.innerHTML="Frist Number"; return false; }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 = "Only letter or number"; return false; } } } //password if(oFpass.value!=oSpass.value){ oError.innerHTML="The password must be the same"; return false; }else if(oFpass.value.length<6||oFpass.value.length>12) { oError.innerHTML = "Password must be 6 to 12"; return false; } return true; // window.alert("Register Successful") }
{% extends 'base.html' %} {% block title %}Register{% endblock %} {% block head %} <link href="{{ url_for('static',filename='CSS/regist.css') }}" rel="stylesheet" type="text/css"> <script src="{{ url_for('static',filename='JScript/regist.js') }}"></script> {% endblock %} {% block main %} <div class="login"> <form action="{{ url_for('regist') }}" method="post"> <div class="login-top"> <h1>Register FORM</h1> <input id="uname" name="username" type="text" value="Username" onfocus="this.value = '';" onblur="if (this.value==''){ this.value = 'Username';}"> <input id="fpass" name="password" type="password" value="Password" onfocus="this.value = '';" onblur="if (this.value==''){ this.value = 'Password';}"> <input id="spass" type="password" value="Password" onfocus="this.value = '';" onblur="if (this.value==''){ this.value = 'Password';}"> </div> <div class="login-bottom"> <div id="errorbox"></div><button onclick="return MyRegister()">Regist</button> </div> </form> </div> {% endblock %}