完成注册功能

  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), nullable=True)

db.create_all()

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

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

@app.route('/zhuce/',methods=['GET','POST'])
def zhuce():
    if request.method=='GET':
        return render_template('zhuce.html')
    else:
        usern=request.form.get('username')
        idn = request.form.get('id')
        passw=request.form.get('password')
        user=User.query.filter(User.username==usern).first()
        if user:
            return u'用户已存在'
        else:
            user1=User(username=usern,id=idn,password=passw)
            db.session.add(user1)
            db.session.commit()
            return  redirect(url_for('denglu'))

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

if __name__ == '__main__':
    app.run(debug=True)
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:123456@localhost:3306/mis?charset=utf8'
SQLALCHEMY_TRACK_MODIFICATIONS = False

注册html

{% extends'shouye.html' %}

{% block title %}
用户注册
{% endblock %}

{% block js %}
 <script src="{{ url_for('static',filename='js/cc.js') }}">
 </script>
{% endblock %}

{% block jiemian %}
<div  id="container" style="width:500px;margin: 0 auto " >
    <form action="{{ url_for('zhuce') }}" method="post">
        <div id="header" style="background-color:mediumaquamarine;height: 40px"><h2 align="center" style="margin-bottom:0;font-size: 33px;font-family: 楷体" >新用户注册</h2></div>
        <div id="content" style="background-color:#EEEEEE;height: 250px;width:500px;float:left;text-align:center;font-size: 22px">
            <div class="input-box">
                <br>用户昵称<input id="un" type="text" name="username" placeholder="请输入昵称"style="height: 20px">
            </div>
            <div class="input-box">
                <br>输入密码<input id="pw" type="password" name="password"placeholder="请输入密码" style="height: 20px"><br>
            </div>
            <div class="input-box">
                <br>确认密码<input id="tpw" type="password" name="pass"placeholder="请确认密码"style="height: 20px"><br>
            </div>
            <div class="input-box">
{#                <br><input  type="submit" value="登录" onclick="myLogin()" style="width:65px;height:25px;font-size:15px">#}

                 <br><button onclick="myLogin()"style="width:65px;height:25px;font-size:15px">注册</button>
             <div id="error_box" style="font-size: 25px"><br></div>
            </div>
        </div>
        <div id="footer" style="background-color:mediumaquamarine;clear:both;text-align:center;height: 40px;font-size: 31px;font-family: 楷体">版权*GZCC</div>
    </form>
</div>
{% endblock %}

 

 

 

 

posted on 2017-11-22 16:01  079刘洁婷  阅读(99)  评论(0编辑  收藏  举报

导航