完成注册功能

Posted 100彭楚殷

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

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)
    nickname = db.Column(db.String(20))
#db.create_all()

# user = User(username=\'micssa\',password=\'213232\')
# db.session.add(user)
# db.session.commit()
#
# user = User(username=\'mic45878\',password=\'2222222\')
# db.session.add(user)
# db.session.commit()     #增加


# user=User.query.filter(User.username==\'micssa\').first()
# print(user.id,user.username,user.password)     #查找

# user=User.query.filter(User.username==\'micssa\').first()
# user.password = \'111111\'
# db.session.commit()         #修改

# user=User.query.filter(User.username==\'micssa\').first()
# db.session.delete(user)
# db.session.commit()        #删除

@app.route(\'/\')
def vi():
    return render_template("base.html")
@app.route(\'/first\')
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:
        usern = request.form.get("newn")
        passw = request.form.get("newp")
        user = User.query.filter(User.username==usern).first()
        if user:
            return "用户名已存在!"
        else:
            user = User(username = usern,password = passw)
            db.session.add(user)
            db.session.commit()
            return redirect(url_for("login"))

if __name__ == \'__main__\':
    app.run(debug=True)
SQLALCHEMY_DATABASE_URI = \'mysql+pymysql://root:@127.0.0.1:3306/mis_db?charset=utf8\'
SQLALCHEMY_TRACK_MODIFICATIONS = False
{% extends \'base.html\' %}
{% block title %}
注册
{% endblock %}
{% block head %}
    <link href="{{url_for("static",filename="css/vv.css") }}" rel="stylesheet" type="text/css">
    <script src="{{ url_for("static",filename="js/new.js") }}"></script>
{% endblock %}
{% block main %}
<body bgcolor="#778899" class="div4">
<div class="div1">
    <h2 class="title"></h2>
    <form action="{{ url_for("regist") }}" method="post">
        <div class="normal-title">
            <a class="div2" href="login.html">登录</a>
            <b>|</b>
            <a  class="div2" href="regist.html">注册</a>
        </div>

    <div class="div3">
        用户:<input id="newname1" type="text" placeholder="请输入昵称" name="newn">
    </div>
    <div class="div3">
        密码:<input id="newpass1" type="text" placeholder="请输入密码" name="newp">
    </div>
    <div class="div3">
        密码:<input id="newpass2" type="text" placeholder="请再次输入密码" name="newp">
    </div>
    <div id="error_box"><br></div>
     <div class="div3">
        <button onclick="myRegister()">Regist</button>
    </div>
    <div class="div2">ByYin</div>
    </form>>
</div>
<br>
</body>
{% endblock %}
</html>
function myRegister() {
     var nName = document.getElementById("newname1");
     var nError = document.getElementById("error_box");
     var nPass =document.getElementById("newpass1");
     var nPass1=document.getElementById("newpass2");
     var isError = true;
     nError.innerHTML="<br>";
     if (nName.value.length < 6||nName.value.length > 20) {
         nError.innerHTML = "用户名为6位到20位!";
         isError = false;
         return isError;
     }
     else if ((nName.value.charCodeAt(0)) >= 48 && nName.value.charCodeAt(0) <= 57) {
         nError.innerHTML = "用户名首字母不能为数字";
         isError = false;
         return isError;
     }
     else for(var i=0;i<nName.value.length;i++){
         if((nName.value.charCodeAt(i)<48||nName.value.charCodeAt(i)>57)&&(nName.value.charCodeAt(i)<97||nName.value.charCodeAt(i)>122)){
             nError.innerHTML="用户名只能为字母或数字!";
             isError = false;
             return isError;
         }
     }
     if (nPass.value.length<6||nPass.value.length>20){
         nError.innerHTML="密码为6-20位!";
         isError = false;
         return isError;
     }
     if(nPass.value!=nPass1.value){
         nError.innerHTML="您输入的密码不一致!请重新输入!";
         isError = false;
         return isError;
     }
     window.alert("您已注册成功!");
     return isError;
 }

 

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

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

带有固定按钮的片段

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

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

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

完成注册功能