码迷,mamicode.com
首页 > 其他好文 > 详细

11.17

时间:2017-11-17 10:50:48      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:port   style   span   nic   不提交   tab   登录   let   www   

完成注册功能

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

py:

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)


class SQLALchemy(object):
    pass


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


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

@app.route(/login/,methods=[GET,POST])
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)
        nickname = request.form.get(nickname)
        user = User.quary.filter(User.username == username).first()
        if user:
            return uusername existed
        else:
            user = User(username=username,password=password,nickname=nickname)
            db.session.add(user)#数据库操作
            db.session.commit()
            return redirect(url_for(login))


if __name__ == __main__:
    app.run()
{% extends daohang.html %}
{% block title %}注册{% endblock %}
{% block head %}
    <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘css/zhuce.css‘) }}">
    <script src="{{ url_for(‘static‘,filename=‘js/zhuce.js‘) }}"></script>
{% endblock %}

{% block main %}
    <div class="box">
        <h2>注册</h2>
        <form action="{{ url_for(‘zhuce‘) }}" method="post">
            <div class="input_box">
                <input id="uname" type="text" placeholder="请输入用户名" name="username">
            </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="fnzhuce()">注册</button>
            </div>
        </form>
    </div>
{% endblock %}

js:

function fnLogin() {
    var oUname = document.getElementById("uname")
    var oName = document.getElementById("nname")
    var oError = document.getElementById("error_box")
    var oUpass = document.getElementById("upass")
    var ooUpass=document.getElementById("uupass")
    oError.innerHTML = "<br>"
    //unname
    if (oUname.value.length < 6 || oUname.value.length > 20) {
        oError.innerHTML = "name:6-200000";
        isError = false;
        return isError;
    } else if ((oUname.value.charCodeAt(0) >= 48) && (oUname.value.charCodeAt(0) <= 57)) {
        oError.innerHTML = "first letter.";
        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 = "only letter or number";
          isError = false;
        return isError;
        }
    }

    //upass
    if (oUpass.value.length < 6 || oUpass.value.length > 12) {
        oError.innerHTML = "密码6-12位"
      isError = false;
        return isError;
    }
    //uupass
    if (ooUpass.value!=oUpass.value) {
        oError.innerHTML = "重复密码错误"
        isError = false;
        return isError;
    }
    //name
    if (oName.value.length < 3 || oUpass.value.length > 6) {
        oError.innerHTML = "昵称2-6位";
      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 = "only letter or number";
            isError = false;
        return isError;
        }
    }
                window.alert("登录成功!")


        }

 

11.17

标签:port   style   span   nic   不提交   tab   登录   let   www   

原文地址:http://www.cnblogs.com/wuyishan/p/7850216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!