完成注册功能

Posted 137陈楚洪

tags:

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

  1. html文件:
    1. <form>中设置 action和method="post"
    2. <input> 中设置 name
  2. 主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重定向到登录页

1、js文件:

function fnEnroll() {
    var zcoUname = document.getElementById("zcuname");
    var zcoError = document.getElementById("zcerror_box");
    var zcoUword1 = document.getElementById("zcupassword1");
    var zcoUword2 = document.getElementById("zcupassword2");
    var isoError = true;
    zcoError.innerHTML = "<br>";
    if (zcoUname.value.length<6|| zcoUname.value.length>12) {
         zcoError.innerHTML = "用户名为6到12位";
         isoError = false;
        return isoError;
    } else if((zcoUname.value.charCodeAt(0)>=48)&&(zcoUname.value.charCodeAt(0)<=57)){
        zcoError.innerHTML = "用户名首位不能是数字";
        isoError=false;
        return isoError;
    } else for (var i = 0; i < zcoUname.value.length; i++) {
        if(zcoUname.value.charCodeAt(i)<48||(zcoUname.value.charCodeAt(i)>57)&&(zcoUname.value.charCodeAt(i)<97)||zcoUname.value.charCodeAt(i)>122){
            zcoError.innerHTML = "用户名只能是字母与数字";return isoError;
        }
    }
    if (zcoUword1.value.length<6||zcoUword1.value.length>20){
        zcoError.innerHTML = "密码为6到20位";
        isoError = false;
        return isoError;
    }
    if (zcoUword1.value!=zcoUword2.value){
        zcoError.innerHTML="两次密码不一致";
        return false;
    }
    return true;
    window.alert("注册成功!!");
}

2、html文件:

{% extends base.html %}
{% block title %}注册{% endblock %}
    {% block head %}
    <link rel="stylesheet" type="text/css" href="../static/css/20.css">
    <script src="../static/JS/注册.js"></script>
    <script src="{{ url_for(‘static‘,filename=‘JS/注册.js‘) }}"></script>
    {% endblock %}
{% block main %}
<body style="background: burlywood">
<div class="box">
    <h1>注册</h1>
    <form action="{{ url_for(‘regist‘) }}" method="post">
    <div class="input_box">
        注册帐号:<input id="zcuname" type="text" placeholder="请输入用户名" name="usename">
    </div>
    <div class="input_box">
        注册密码:<input id="zcupassword1" type="password" placeholder="请输入密码" name="password">
    </div>
     <div class="input_box">
         再次输入:<input id="zcupassword2" type="password" placeholder="请再次输入密码" >
    </div>
    <div id="zcerror_box"><br></div>
    <div class="input_box">
        <button type=submit onclick="fnEnroll()">立即注册</button> <a href="login.html">已注册</a> </div></from> </div> </body> {% endblock %}

3、主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=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 base():
    return render_template(base.html)


@app.route(/login/)
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 username existed
        else:
            user=User(username=username,password=password)
            db.session.add(user)
            db.session.commit()
            return redirect(url_for(login))




@app.route(/zimoban/)
def zimoban():
    return render_template(zimoban.html)


@app.route(/zzimoban/)
def zzimoban():
    return render_template(zzimoban.html)


@app.route(/index/)
def index():
    return render_template(index.html)


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

 

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

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

带有固定按钮的片段

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

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

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

完成注册功能