完成注册功能

  1. js文件: onclick函数return True时才提交表单,return False时不提交表单。
  2. html文件:
    1. <form>中设置 action和method="post"
    2. <input> 中设置 name
  3. 主py文件中:
    1. from flask import  request, redirect, url_for
    2. @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 oCheckpass = document.getElementById("checkpass");
    oError.innerHTML="<br>";

    if(oUname.value.length<6||oUname.value.length>20){
        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(oUpass.value.length<6||oUpass.value.length>20){
        oError.innerHTML="密码长度为6-12位";
        return false;
    }

    if(oCheckpass.value!=oUpass.value){
        oError.innerHTML="密码不一致" ;
        return false;
    }else if(oCheckpass.value==""){
        oError.innerHTML="未确认密码";
        return false;
    }

    window.alert("注册成功");
    return true;
}
{% extends 'index.html' %}

{% block zhucetitle %}注册{% endblock %}
{% block zhucehead %}
    <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/login.css') }}">
    <script src="{{ url_for('static',filename='js/login.js') }}"></script>
{% endblock %}
{% block zhucebody %}
    <div id="container" style="margin-left: auto;margin-right: auto">
        <div id="header"><h2 align="center">注册</h2></div>
        <div id="content">
            <form action="{{ url_for('zhuce') }}" method="post">
                <p align="center">Username:</p>
                <p align="center">
                    <input type="text" name="username" id="user" placeholder="请输入用户名">
                </p>
                <p align="center">Password:</p>
                <p align="center">
                    <input type="password" name="password" id="pass" placeholder="请输入密码">
                </p>
                <p align="center">Lnput again:</p>
                <p align="center">
                    <input type="password" name="again" id="again" placeholder="再次输入密码">
                </p>
                <p align="center">
                    <input type="radio" name="r1" id="r1" value="stu">student
                    <input type="radio" name="r2" id="r2" value="tea">teacher
                </p>
                <div id="error_box"><br></div>
                <p align="center">
                    <input type="button" value="registration" οnclick="fnRegistration()">
                </p>

            </form>
        </div>
        <div id="footer"><p align="right">版权@misaki</p></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()

@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('username')
        passw=request.form.get('password')
        user=User.query.filter(User.user==usern).first()
        if user:
            return 'user 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)

 

转载于:https://www.cnblogs.com/yangjlee/p/7873022.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值