完成注册功能
- 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重定向到登录页
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 home(): return render_template('index.html') @app.route('/signin/',methods=['GET','POST']) def sign_in(): return render_template('denglu1.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 u'username existed.'
else:
user=User(username=username,password=password)
db.session.add(user)
db.session.commit()
return rediect(url_for('login'))
if __name__ == '__main__':
app.run()
@app.route('/question/') def question(): return render_template('question.html') if __name__ == '__main__': app.run(debug=True)
function fnLogin(){ var oNewname = document.getElementById("uname"); var oNewpass = document.getElementById("upass"); var oAgainname = document.getElementById("aupass"); var oError = document.getElementById("error_box"); var isError=true; oError.innerHTML="<br>" if(oNewname.value.length<6 && oNewname.value.length>12) { oError.innerHTML = "用户名需在6-12位中间"; isError=false; return isError; } else if ((oNewname.value.charCodeAt(0)>=48)&&(oNewname.value.charCodeAt(0)<=57)){ oError.innerHTML ="用户名开头不能为数字"; isError=false; return isError; } else for(var i=0;i<oNewname.value.length;i++){ if((oNewname.value.charCodeAt(i)<48 || oNewname.value.charCodeAt(i)>57)&&(oNewname.value.charCodeAt(i)<97 || oNewname.value.charCodeAt(i)>122)){ oError.innerHTML = "用户名只能为数字或字母"; isError=false; return isError; } } //密码 if(oNewpass.value.length>12|| oNewpass.value.length<6){ oError.innerHTML="密码需在6-12位中间"; isError=false; return isError; } else if(oNewpass.value != oAgainname.value) { oError.innerHTML="两次密码输入不一致"; isError=false; return isError; } window.alert("注册成功!") }
{% extends'index.html' %} {% block head %} <link rel="stylesheet" href="{{ url_for('static',filename='css/logsign.css')}}" type="text/css"> <script src="{{ url_for('static',filename='js/signin.js') }}"></script> {% endblock %} {% block main %} <body> <div class="pen-title"> <h1>注 册</h1> </div> <div class="from"> <h2>请进行注册</h2> <div class="input_box"> <input id="uname" type="text" placeholder="请设置您的用户名" name="username"> </div> <br> <div class="input_box"> <input id="upass" type="password" placeholder="请输入密码" name="password"> </div> <div class="input_box"> <input id="aupass" type="password" placeholder="请再次输入密码"> </div> <div id="error_box"><br></div> <div class="login_box"> <button onclick="fnLogin()">注册</button> </div> </div> {% endblock %} </body> </html>