YWEIEN

导航

完成注册功能

  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, session

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()





@app.route('/')
def index():
    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('username')

        nickn = request.form.get('nickname')

        passw = request.form.get('password')

        user = User.query.filter(User.username == usern).first()

        if user:

            return u'username existed.'

        else:

            user1 = User(username=usern, password=passw, nickname=nickn)

            db.session.add(user1)

            db.session.commit()

            return redirect(url_for('login'))


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


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

 

function fnLogin(){
            var oNewname = document.getElementById("uname");
            var oNewpass = document.getElementById("upass");
            var oAgainname = document.getElementById("aupass");
            var oError = document.getElementById("error_box");
            var isError=true;
            oError.innerHTML="<br>"
            if(oNewname.value.length<6 && oNewname.value.length>12) {
                oError.innerHTML = "用户名需在6-12位中间";
                isError=false;
                return isError;
            }
           else if ((oNewname.value.charCodeAt(0)>=48)&&(oNewname.value.charCodeAt(0)<=57)){
                oError.innerHTML ="用户名开头不能为数字";
               isError=false;
                return isError;
            }
            else for(var i=0;i<oNewname.value.length;i++){
                if((oNewname.value.charCodeAt(i)<48 || oNewname.value.charCodeAt(i)>57)&&(oNewname.value.charCodeAt(i)<97 || oNewname.value.charCodeAt(i)>122)){
                    oError.innerHTML = "用户名只能为数字或字母";
                   isError=false;
                   return isError;
                }
                }
            //密码
            if(oNewpass.value.length>12|| oNewpass.value.length<6){
                oError.innerHTML="密码需在6-12位中间";
               isError=false;
                return isError;
            }
            else if(oNewpass.value != oAgainname.value) {
                oError.innerHTML="两次密码输入不一致";
               isError=false;
                return isError;
            }
            window.alert("注册成功!")
        }

 

 

 

 

{% extends'index.html' %}
{% block head %}
    <link rel="stylesheet" href="{{ url_for('static',filename='css/logsign.css')}}" type="text/css">
    <script src="{{ url_for('static',filename='js/signin.js') }}"></script>
{% endblock %}
{% block main %}
<body>
<div class="pen-title">
  <h1>注 册</h1>
</div>
<div class="from">
    <h2>请进行注册</h2>
    <div class="input_box">
       <input id="uname" type="text" placeholder="请设置您的用户名" name="username">
    </div>
    <br>
    <div class="input_box">
        <input id="upass" type="password" placeholder="请输入密码" name="password">
    </div>
    <div class="input_box">
        <input id="aupass" type="password" placeholder="请再次输入密码">
    </div>
    <div id="error_box"><br></div>
    <div class="login_box">
        <button onclick="fnLogin()">注册</button>
    </div>
</div>
{% endblock %}
</body>
</html>

 

posted on 2017-11-22 19:42  YWEIEN  阅读(168)  评论(0编辑  收藏  举报