完成注册功能
- 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重定向到登录页
{% extends 'basic.html' %} {% block title %}注册{% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='../static/css/20.css') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='../stati/ccss/200.css') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='../static/css/2000.css') }}"> <script src="{{url_for('static',filename='../static/js/aaa.js') }}" type="text/css"></script> {% endblock %} {% block rege %} <script src="../static/js/aaa.js"></script>> </head> <body class="abc"> <h1 align="center" class="ads">Welcome to gzcc</h1> <h4 align="center"class="ads"> Management system</h4> <div style="margin:0" align="center"> <div id="container" style="width:400px " > <div class="touming" id="header" style="background-color: aqua;color: darkcyan;"><h2 align="center" style="margin-bottom:0;"> CREATE ACCOUNT</h2></div> <form align="center" > <input id="uname" type="text"name="username"placeholder="someone@example.com"><br> <input id="upass" type="password"name="password" placeholder="create password"><br> <input id="oupass" type="password"name="apassword" placeholder="again password"><br> <input id="unickname" type="text"name="nickname"placeholder="nickname"><br> <a href={{ url_for('login') }}>Log in </a> <div id="error_box"> <br></div> <input class="body" type="button" onclick=" return myLoginn()" value="CREATE"style="width: 250px;height: 50px;background-color: aqua;"> <div id="content" style=";height:150px;width:400px;float:left;"></div> <div class="touming" id="footer" style="background-color:aqua;clear:both;text-align:center;color: darkcyan;">版权 © djc</div> </form> </div> </div> </body> </html>{% endblock %}
from flask import Flask from flask_sqlalchemy import SQLAlchemy import config from flask import Flask,render_template,request 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(20)) db.create_all() @app.route('/') def index(): return render_template('index.html') @app.route('/login/') def login(): return render_template('login.html') @app.route('/rege/',methods=['GET','POST']) def rege(): if request.method =='GET': return render_template('rege.html') else: usern = request.form.get('username') nickn =request.form.get('nickname') 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,niciname = nickn) db.session.add(user1) db.session.commit() @app.route('/question/') def question(): return render_template('question.html') if __name__=="__main__": app.run(debug=True)
function myLoginn(){ var oUname = document.getElementById("uname"); var oError = document.getElementById("error_box"); var oUpass = document.getElementById("upass"); var ooUpass= document.getElementById("oupass"); var isError=true; oError.innerHTML = "<br>" //oUname if (oUname.value.length>12 || oUname.value.length<6){ oError.innerHTML = "用户名为6到12位数"; isError = false; return isError; } else if((oUname.value.charCodeAt(0)>=48) && oUname.value.charCodeAt(0)<=57){ oError.innerHTML = "首字母不能是数字"; isError = false; return isError; } 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(upass.value.length>12 || upass.value.length<6){ oError.innerHTML="密码为6到12位数"; isError = false; return isError; } if(oUpass.value != ooUpass.value){ oError.innerHTML="两次密码输入不一致"; isError = false; return isError; } window.alert("已成功注册") }