1 from flask import Flask, render_template, request, redirect, url_for, session 2 from flask_sqlalchemy import SQLAlchemy 3 import config 4 from functools import wraps 5 from datetime import datetime 6 7 app = Flask(__name__) 8 app.config.from_object(config) 9 db = SQLAlchemy(app) 10 11 12 class User(db.Model): 13 __tablename__ = 'user' 14 id = db.Column(db.Integer, primary_key=True, autoincrement=True) 15 username = db.Column(db.String(20), nullable=False) 16 password = db.Column(db.String(20), nullable=False) 17 nickname = db.Column(db.String(20), nullable=True) 18 19 class Question(db.Model): 20 __tablename__ = 'question' 21 id = db.Column(db.Integer, primary_key=True, autoincrement=True) 22 author_id=db.column(db.Integer,db.ForeignKey('user.id')) 23 title=db.Column(db.String(100), nullable=False) 24 detail=db.Column(db.Text, nullable=False) 25 creat_time=db.column(db.DateTime,default=datetime.now) 26 author=db.relationship('User',backref=db.backref('question')) 27 28 db.create_all() #是否链接成功 29 30 31 # 数据添加 32 # user=User(username='ma',password='gg') 33 # db.session.add(user) 34 # db.session.commit() 35 # 36 # 37 # 数据更新 38 # 39 # user = User.query.filter(User.username=='hh').first() 40 # user.password='gg' 41 # db.session.commit() 42 # 43 # 44 # 数据查询 45 # user = User.query.filter(User.username=='ma').first() 46 # print(user.username,user.password) 47 # 48 # 数据删除 49 # user = User.query.filter(User.username=='ma').first() 50 # db.session.delete(user) 51 # db.session.commit() 52 53 54 # session会话连接 55 # filter()过滤器 56 # route制定路径和函数之间的关系 57 # def定义一个变量 58 59 @app.route('/') 60 def switch(): 61 return render_template('switch.html') 62 63 64 @app.route('/login/', methods=['GET', 'POST']) # 跳转登陆,methods定义它有两种请求方式 65 def login(): 66 if request.method == 'GET': 67 return render_template('login.html') 68 else: 69 username = request.form.get('user') 70 password = request.form.get('psw') 71 user = User.query.filter(User.username == username).first() # 判断用户名是否存在 72 if user: 73 if user.password == password: 74 session['user']=username 75 session.permanent = True 76 return redirect(url_for('switch')) 77 else: 78 return '用户名不存在' 79 @app.context_processor 80 def mycontext(): 81 username=session.get('user') 82 if username: 83 return {'username':username} 84 else: 85 return {} 86 87 def loginFirst(func): 88 @wraps(func) 89 def wrapper(*args,**kwargs): 90 if session.get('user'): 91 return func(*args,**kwargs) 92 else: 93 return redirect(url_for('login')) 94 return wrapper 95 96 # @app.route('/question/', methods=['GET', 'POST']) 97 # @loginFirst 98 # def question(): 99 # if request.method=='GET': 100 # return render_template('question.html') 101 # else: 102 # title=request.form.get('title') 103 # detail=request.form.get('detail') 104 # author_id=User.query.filter(User.username==session.get('user')).first().id 105 # question=Question(title= title,detail=detail,quthor_id=author_id) 106 # db.session.add( question ) 107 # db.session.comit() 108 # return (url_for('index')) 109 110 @app.route('/register/', methods=['GET', 'POST']) # 跳转注册,methods定义它有两种请求方式 111 def register(): 112 if request.method == 'GET': 113 return render_template('register.html') 114 else: 115 116 username = request.form.get('user') 117 password = request.form.get('psw') 118 nickname = request.form.get('nickname') 119 user = User.query.filter(User.username == username).first() # 判断用户名是否存在 120 if user: 121 return u'该用户已存在' 122 else: 123 user = User(username=username, password=password, nickname=nickname) 124 db.session.add(user) 125 db.session.commit() 126 return redirect(url_for('login')) 127 128 @app.route('/logout/') 129 def logout(): 130 session.clear() 131 return render_template('switch.html') 132 133 @app.route('/wenda/') 134 def wenda(): 135 return render_template('wenda.html') 136 137 138 if __name__ == '__main__': 139 app.run(debug=True)
转载于:https://www.cnblogs.com/z1-z/p/7912382.html