完成注册功能
- 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重定向到登录页
py主文件
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)) #增 #user = User(username ='ccccls',password='111111' ) #db.session.add(user) #db.session.commit() #查 #user=User.query.filter(User.username =='ccccls').first() #print(user.username,user.password) #删 #user=User.query.filter(User.username =='ccccls').first() #db.session.delete(user) 3db.session.commit() #改 #user=User.query.filter(User.username =='ccccls').first() #user.password='ccccls' #db.session.commit() #db.create_all() @app.route('/base/') def base(): return render_template('base.html') @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') nickn = request.form.get('nickname') passw= 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 redirect(url_for('login')) @app.route('/question/') def question(): return render_template('question.html') @app.route('/index/') def index(): return render_template('index.html') if __name__ == '__main__': app.run(debug=True)
html页面
{% extends'shouye.html' %} {% block title %} register {% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="register.css"> <script src="login.js.js"></script> {% endblock %} {% block main %} <body> <div class="bigbox"> <div class="box"> <h2>♛游民星空账号注册♛</h2> <div class="input_box"> 输入账号:<input id="umane"type="text"placeholder="请输入账号"> </div> <div class="input_box"> 输入密码:<input id="upass"type="password"placeholder="请输入密码"> </div> <div class="input_box"> 再输密码:<input id="userpass1" type="password" placeholder="请再次输入密码"> </div> <div id="error_box"><br></div> <div class="input_box"> <h5><input type="radio" value="我已阅读相关条例">我已阅读相关条例</h5>. <br> <button onclick="myLogin()">注册</button> <button onclick="myLogin()">取消</button> </div> </div> </body> {% endblock %
JS页面
function myLogin() { var oUname = document.getElementById("umane"); var oError = document.getElementById("error_box"); var oUpass = document.getElementById("upass"); var oUpass1= document.getElementById("userpass1"); oError.innerHTML = "<br>" //oUname if (oUname.value.length>12 || oUname.value.length<6){ oError.innerHTML = "账号长度为6-12位"; return; } 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 = "账号中含有非法字符"; return; } } if(upass.value.length>12 || upass.value.length<6){ oError.innerHTML="password;6-12"; return; } if(oUpass.value != oUpass1.value){ oError.innerHTML="两次密码输入不一致"; return; } window.alert("注册成功 !")