完成注册功能
Posted 熊锋阳
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成注册功能相关的知识,希望对你有一定的参考价值。
- 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重定向到登录页
function fnLogin() { var oUname = document.getElementById("uname"); var oError = document.getElementById("error-box"); var oUpass = document.getElementById("upass"); var oPass1 = document.getElementById("upass1"); var isoError = true; oError.innerHTML = "<br>"; if (oUname.value.length<6||oUname.value.length>20){ oError.innerHTML="用户名要6-20位"; isoError = false; return isoError; } else if(oUname.value.charCodeAt(0)>=48&&(oUname.value.charCodeAt(0)<=57)){ oError.innerHTML = "用户名首字母必须是字母"; isoError = false; return isoError; } 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 isoError; } } if (oUpass.value.length<6||oUpass.value.length>20){ oError.innerHTML="密码至少6-20位"; isoError = false; return isoError; } if (oUpass != oPass1) { oError.innerHTML = "两次密码输入不一致" isoError = false; return isoError; } window.alert("注册成功") return true; }
{% extends ‘base.html‘ %} {% block registtitle %}注册{% endblock %} {% block registhead %} <script src="../static/js/regist.js"></script> {% endblock %} {% block registbody %} <div class="bigbox"> <div class="box"> <h1>注册</h1> <form action="{{ url_for(‘regist‘) }}" method="post"></form> <div class="input-box"> 用户账号:<input id="uname" type="text" placeholder="请输入用户名"name="usename"> </div> <div class="input-box"> 注册密码:<input id="upass" type="password" placeholder="请输入密码"name="password"> </div> <div class="input-box"> 确认密码:<input id="upass1" type="password" placeholder="请再次输入密码"> </div> <div id="error-box"><br></div> <div class="input-box"> <button onclick="fnLogin()">确认注册</button> </div> </div> </div> {% endblock %}
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() #数据的添加方法 #user=User(username=‘kobe24‘,password=‘666666‘) #db.session.add(user) #db.session.commit() #数据的查询方法 #user=User.query.filter(User.username ==‘kobe24‘).first() #print(user.username,user.password) # 数据的修改方法 #user = User.query.filter(User.username==‘kobe24‘).first #user.password=‘123456‘ #db.session.commit() # 数据的删除方法 #user = User.query.filter(User.username==‘kobe24‘).first() #db.session.delete(user) #db.session.commit() @app.route(‘/‘) def hello_world(): return render_template(‘base.html‘) @app.route(‘/login‘,methods=[‘GET‘,‘POST‘]) def login(): return render_template(‘login.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 redirect(url_for(‘login‘)) @app.route(‘/fabu‘) def fabu(): return render_template(‘fabu.html‘) if __name__ == ‘__main__‘: app.run(debug=True)
以上是关于完成注册功能的主要内容,如果未能解决你的问题,请参考以下文章