完成注册功能

Posted 蒋宇翔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成注册功能相关的知识,希望对你有一定的参考价值。

  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重定向到登录页

{% extends ‘daohang.html‘ %}
    {% block registtitle %}注册{% endblock %}
    {% block registhead %}
    <script src="../static/js/zhuce.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 %}

JS:

function zhuce() {
    var oPass = document.getElementById("pass");
    var oPass1 = document.getElementById("pass1");
    var oUname = document.getElementById("name");
    var oError = document.getElementById("error_box")
    var isError = true;
    oError.innerHTML = "<br>"
    if (oUname.value.length < 6 || oUname.value.length > 12) {
        oError.innerHTML = "用户名6-12位";
        isError = false;
        return isError;
    } else if ((oUname.value.charCodeAt(0) >= 48) && (oUname.value.charCodeAt(0) <= 57)) {
        oError.innerHTML = "首位为英文";
        isError = false;
        return isError;
    } 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 = "字母或数字";
                isError = false;
                return isError;
            }

        }
    if (oPass.value.length < 6 || oPass.value.length > 12) {
        oError.innerHTML = "密码6-12位";
        isError = false;
        return isError;
    }
    if (oPass != oPass1) {
        oError.innerHTML = "两次密码输入不一致"
        isError = false;
        return isError;
    }
    window.alert("注册成功")
    return true;
}

PY:

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=TabError,autoincrement=True)
    username = db.Column(db.String(20), nullable=False)
    password = db.Column(db.String(20), nullable=False)
#增加
user = User(username =1,password=1234 )
db.session.add(user)
db.session.commit()
#查询
user=User.query.filter(User.username ==1).first()
print(user.username,user.password)
#删除
user=User.query.filter(User.username ==1).first()
db.session.delete(user)
db.session.commit()
#修改
user=User.query.filter(User.username ==1).first()
user.password=123456789
db.session.commit()
@app.route(/)
def hello_world():
    return render_template(daohang.html)


def base():
    return render_template(daohang.html)


@app.route(/denglu,methods=[GET,POST])
def login():
    return render_template(denglu.html)


@app.route(/zhuce,methods=[GET,POST])
def regist():
    if request.method == GET:
        return render_template(zhuce.html)
    else:
        username = request.form.get(username)
        password = request.form.get(password)

        user = User.query.filter(User.username == username).first()
        if user:
            return uusername existed
        else:
            user = User(username=username, password=password)
            db.session.add(user)
            db.session.commit()
            return redirect(url_for(login))



@app.route(/fabu/,methods=[GET,POST])
def zimoban():
    return render_template(fabu.html)




if __name__ == __main__:
    app.run(debug=True)

 

以上是关于完成注册功能的主要内容,如果未能解决你的问题,请参考以下文章

解决方案电影标题中缺少代码的片段,完成挑战更多[关闭]

带有固定按钮的片段

从零开始配置vim(27)——代码片段

从零开始配置vim(27)——代码片段

从零开始配置vim(27)——代码片段

完成注册功能