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

 1 from flask import Flask,render_template,request,redirect,url_for,session
 2 from flask_sqlalchemy import SQLAlchemy
 3 import config
 4 
 5 app = Flask(__name__)
 6 app.config.from_object(config)
 7 db = SQLAlchemy(app)
 8 
 9 class User(db.Model):
10     __tablename__ = 'user'
11     id = db.Column(db.Integer,primary_key=True,autoincrement=True)
12     username = db.Column(db.String(20),nullable=False)
13     password = db.Column(db.String(20),nullable=False)
14     nickname = db.Column(db.String(50))
15 
16 # user = User(username = 'misjiaojiao',password = 'misjiaojiao')
17 # db.session.add(user)
18 # db.session.commit()
19 
20 # user = User.query.filter(User.username == 'misjiaojiao').first()
21 # db.session.delete(user)
22 # db.session.commit()
23 
24 # user = User.query.filter(User.username == 'misjiaojiao').first()
25 # user.password='1234657'
26 # db.session.commit()
27 
28 db.create_all()
29 
30 @app.route('/')
31 def index():
32     return render_template('switch切换.html')
33 
34 @app.route('/login/', methods = ['GET','POST'])
35 def login():
36     return render_template('login登录.html')
37 
38 @app.route('/regist/', methods = ['GET','POST'])
39 def regist():
40     if request.method == 'GET':
41         return render_template('regist注册.html')
42     else:
43         username = request.form.get('username')
44         password = request.form.get('password')
45         nickname = request.form.get('nickname')
46         user = User.query.filter(User.username == username).first()
47         if user:
48             return 'username existed'
49         else:
50             user = User(username = username,password = password,nickname = nickname)
51             db.session.add(user)  # 数据库操作
52             db.session.commit()
53             return redirect(url_for('login'))
54 
55 
56 
57 
58 if __name__ == '__main__':
59     app.run(debug=True)

 

posted on 2017-11-17 09:52  016李云基  阅读(119)  评论(0编辑  收藏  举报