完成注册功能

from flask import Flask, render_template,request,redirect,url_for,session
from flask_sqlalchemy import SQLAlchemy
# import config
from functools import wraps
from datetime import datetime

app = Flask(__name__)

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('test.html')


@app.route('/login',methods=['GET','POST'])
def login():
    return render_template('10.31.html')

@app.route('/regist',methods=['GET','POST'])
def regist():
    if request.method == 'GET':
        return render_template('注册.html')
    else:
        username = request.form.get('username')
        password = request.form.get('password')
        nickname = request.form.get('nickname')
        user = User.query.filter(User.username == username).first()
        if user:
            return '用户名已存在'
        else:
            user = User(username = username,password = password,nickname = nickname)
            db.session.add(user)
            db.session.commit()
            return render_template(url_for('login'))

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

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

  

{% extends 'test.html' %}

{% block zhucetitle %}regist{% endblock %}
{% block zhucehead %}
    <link rel="stylesheet" type="text/css" href="../static/css/注册.css">
    <script src="../static/js/注册.js"></script>
{% endblock %}

{% block zhucebody %}
<div class="box">
    <h1>注册</h1>
    <form action="{{ url_for('regist')}}" 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="upass" type="password" placeholder="请再次输入密码">
        </div>
        <div id="error-box"><br></div>
        <div class="input-box">
            <button onclick="fnLogin()">确认注册</button>
        </div>
    </form>
</div>
{% endblock %}

  

posted on 2017-11-17 09:52  021杨光哈哈哈  阅读(87)  评论(0编辑  收藏  举报

导航