- 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,session 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)) #db.create_all() #增加数据 user=User(username = 'mis1234',password = '111111') db.session.add(user) db.session.commit() #查询数据 user=User.query.filter(user.username == 'mis1234').first() print(user.id,user.username,user.password) #修改数据 user=User.query.filter(user.username == 'mis3333').first() user.password = '0000' db.session.commit() #删除数据 user=User.query.filter(user.username == 'mis3333').first() db.session.delete(user) db.session.commit()
@app.route('/')
def base():
return render_template('base.html')
@app.route('/login/')
def login():
return render_template('login.html')
@app.route('/zhuche/', methods=['GET','POST'])
def login ():
if request.method =='GET':
return render_template('zhuce.html')#跳转注册
else:
usern = request.form.get('username')
passw = request.form.get('password')
nickn = request.form.get('nickname')
user = User.query.filter(User.username == usern).first()
if user:
return u'username existed'
else:
user1 = User(username = usern,password = passw,nickname=nickn)
db.session.add(user1)#数据库操作
db.session.commit()
return redirect(url_for('gg'))
@app.route('/question/')
def question():
return render_template('question.html')
if __name__ == '__main__': app.run(debug=True)
function fnRegister() { var oUname = document.getElementById("uname"); var oError = document.getElementById("error_box"); var oUiphone = document.getElementById("uiphone"); var oUpass = document.getElementById("upass"); var oUpass1 = document.getElementById("upass1"); var oUpass = document.getElementById("upass").value; var oUpass1 = document.getElementById("upass1").value; if (oUpass1!=oUpass){ document.getElementById("error_box"),innerHTML="两次密码不一致"; return false;} return true; oError.innerHTML = "<br>" if (oUname.value.length < 6 || oUname.value.length > 12) { 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 (oUpass.value.length < 6 || oUpass.value.length > 12) { oError.innerHTML = "密码6-12位"; return; } if (oUpass1.value != oUpass.value) { oError.innerHTML = "rewrite your PIN"; return; } if (oUpass!= oUpass1) { document.getElementById("error_box").innerHTML = "两次密码不一样"; return; } window.alert("注册成功") }
HTML文件:
{% extends 'base.html' %}
{% block zhucetitle %}注册{% endblock %}
{% block zhucehead %}
<title>Registration</title>
<link rel="stylesheet" type="text/css" href="../static/css/zhuce.css">
<script src="../static/js/zhuce.js"></script>
{% endblock %}
{% block zhucebody %}
<div class="box">
<h2>注册</h2>
<form action="{{ url_for('login') }}" method="post">
<div class="input_box">
<input type="text" id="user" placeholder="请输入用户名" name="username">
</div>
<div class="input_box">
<input type="password" id="upass" placeholder="请输入密码"name="password">
</div>
<div class="input_box">
<input type="password" id="upass1" placeholder="请再次确认密码">
</div>
<div id="error_box"><br>
</div>
<div class="input_box">
<button onclick="fnRegistration()">Enter</button>
</div>
</form>
</div>
{% endblock %}