完成注册功能
Posted GT3
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成注册功能相关的知识,希望对你有一定的参考价值。
1.js文件: onclick函数return True时才提交表单,return False时不提交表单。
2.html文件:
<form>中设置 action和method="post"
<input> 中设置 name
3.主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重定向到登录页
from flask import Flask,render_template from flask_sqlalchemy import SQLALchmey import configparser 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(‘regist.html‘) @app.route(‘/regist/‘,methods=[‘GET‘,‘POST‘]) def regist(): if request.method == ‘GET‘: return render_template(‘regist.html‘) else: usern = request.from.get(‘username‘) nickn = request.from.get(‘nickname‘) passw = request.from.get(‘password‘) user = User.query.filter(User.username == usern).first() if user: if user.password == passw: return redirect(url_for(‘index‘)) else: return u‘password error‘ eles: return u‘username is not existed.‘
else: user1 = User(username=usern, password=passw) db.session.add(user1) db.session.commit() return redirect(url_for(‘login‘)) if __name__ == ‘__main__‘: app.run(debug=True)
{% extends‘base.html‘ %} {% block title %} 注册 {% endblock %} {% block head %} <link rel="stylesheet"type="text/css"href="{{url_for(‘static‘,filename="css/zhuce.css") }}"> <script src="{{url_for(‘static‘,filename="js/zhuce.js") }}"></script> {% endblock %} {% block main %} <body> <div class="aa" > <div class="zhuce" ><h2>新用户注册</h2></div> <form action="{{ url_for(‘zhuce‘)}}" method="post"> <div class="aa1" > 请输入账号:<input id="name" type="text"placeholder="请输入用户名"><br> 请输入密码:<input id="password" type="password"placeholder="请输入密码"><br> 请确认密码:<input id="password2" type="password2"placeholder="请再次输入密码"><br> </div> <div id="error_box"><br></div> <div class="aa2" > <button onclick="myLogin()">注册</button> </div> </div> </body> {% endblock %}
function myLogin() { var oUname = document.getElementById("name") var oError = document.getElementById("error_box") var opassword = document.getElementById("password") var opassword2 = document.getElementById("password2") 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 false; } } if(opassword.value.length<6||opassword.value.length>12){ oError.innerHTML="密码需为6-12位"; return false; } if(opassword2.value!=opassword.value){ oError.innerHTML="密码不一致"; return false; } return true; //window.alert("注册成功!")
以上是关于完成注册功能的主要内容,如果未能解决你的问题,请参考以下文章