完成注册功能
- 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重定向到登录页
zhuce.html
{% extends 'base.html' %} {% block title %}注册{% endblock %} {% block head %} <link rel="stylesheet" href="{{ url_for('static',filename='css/login.css') }}"> <script src="{{url_for('static',filename='js/login.js') }}"></script> {% endblock %} {% block main %} <div class="Outbox"> <div class="box"> <h2>用户注册</h2> <form method="post" action="{{ url_for('zhuce') }}"> <div class="input_box"> 输入账号:<input id="name"type="text"placeholder="请输入用户名">name="username"><br> </div> <div class="input_box"> 输入密码:<input id="password"type="password"placeholder="请输入密码">name="password"><br> </div> <div class="input_box"> 确认密码:<input id="password1" type="password" placeholder="请再次输入密码"> </div> <div id="error_box"><br></div> <div class="input_box"> <button onclick="myLogin()">注册</button> <button onclick=window.alert("取消注册") type="submit" class="btn btn-primary btn-block btn-large">返回</button> </div> </div> {% endblock %}
zhuce.js
function myLogin(){ var oUname = document.getElementById("name"); var oError = document.getElementById("error_box"); var oUpass = document.getElementById("password"); var oUpass1= document.getElementById("password1"); oError.innerHTML = "<br>" //oUname if (oUname.value.length>12 || oUname.value.length<6){ oError.innerHTML = "用户名为6到12位数"; return false; } else if((oUname.value.charCodeAt(0)>=48) && oUname.value.charCodeAt(0)<=57){ oError.innerHTML = "首字母不能是数字"; 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 = "只能包含字母和数字"; return false; } } if(upass.value.length>12 || upass.value.length<6){ oError.innerHTML="密码为6到12位数"; return false; } if(oUpass.value != oUpass1.value){ oError.innerHTML="两次密码输入不一致"; return true; } window.alert("已成功注册") }
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): __table__name = '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() @app.route('/') def index(): return render_template('index.html') @app.route('/login/',methods=['GET','POST']) def login(): return render_template('login.html') @app.route('/zhuce/',methods=['GET','POST']) def zhuce(): if request.method =='GET': return render_template('zhuce.html') else: usern = request.form.get('name') teln = request.form.get('password') passw = request.form.get('password1') user = User.query.filter(User.username==usern).first() if user: return 'username existed' else: user= User(username=usern,tel=teln,password=passw) db.session.add(user) db.session.commit() return redirect(url_for('login')) @app.route('/question/',methods=['GET','POST']) def question(): return render_template('question.html') if __name__ == '__main__': app.run(debug=True)