from flask import Flask,render_template,request,redirect,url_for,sessionfrom flask_sqlalchemy import SQLAlchemyfrom functools import wrapsfrom datetime import datetimeimport config,osapp=Flask(__name__)app.config.from_object(config)db=SQLAlchemy(app)class User(db.Model): __tablename__='user' id=db.Column(db.Integer,primary_key=TabError,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)class Question(db.Model): __tablename__='wenda' id=db.Column(db.Integer,primary_key=TabError,autoincrement=True) title=db.Column(db.String(100),nullable=False) detail=db.Column(db.Text,nullable=False) creat_time=db.Column(db.DateTime,default=datetime.now) author_id=db.Column(db.Integer,db.ForeignKey('user.id')) author = db.relationship('User',backref=db.backref('wenda'))# db.create_all() #是否連接成功# 數據添加user=User(username='ma',password='gg')db.session.add(user)db.session.commit()# 數據查找user = User.query.filter(User.username=='hh').first()user.password='gg'db.session.commit()# 數據查詢user = User.query.filter(User.username=='ma').first()print(user.username,user.password)# 數據刪除user = User.query.filter(User.username=='ma').first()db.session.delete(user)db.session.commit()# session會話鏈接# filter()過濾器# route制定路徑和函數之間的關係# def定義一個變量@app.route('/')def switch(): return render_template('switch.html')def index(): context = { 'question': Question.query.order_by('-creat_time').all() } return render_template('index.html',**context)@app.route('/detail/<question_id>')def detail(question_id): return render_template('detail.html',ques = question_id)@app.route('/login/',methods=['GET','POST'])# 跳轉登錄,methods定義它有兩種請求方式def login(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('user') password = request.form.get('psw') user = User.query.filter(User.username == username).first() # 判斷用戶名是否存在 if user: if user.password==password: session['user'] = username session.permanent = True return redirect(url_for('switch')) else: return u'密碼錯誤' else: return u'用戶名不存在'@app.context_processordef mycontext(): username=session.get('user') if username: return {'username':username} else: return{}@app.route('/logout')def logout(): session.clear() return redirect(url_for('switch'))@app.route('/register/',methods=['GET','POST'])# 跳轉註冊,methods定義它有兩種請求方式def register(): if request.method == 'GET': return render_template('register.html') else: username = request.form.get('user') password = request.form.get('psw') nickname = request.form.get('nickname') user = User.query.filter(User.username == username).first() # 判斷用戶名是否存在 if user: return u'該用戶已存在' else: user = User(username=username,password=password,nickname=nickname) db.session.add(user) db.session.commit() return redirect(url_for('login'))# 進入問答界面先登陸功能def loginFirst(func): @wraps(func) def wrapper(*args,**kwargs): if session.get('user'): return func(*args,**kwargs) else: return redirect(url_for('login')) return wrapper@app.route('/wenda/',methods=['GET','POST'])@loginFirstdef wenda(): if request.method=='GET': return render_template('wenda.html') else: title=request.form.get('title') detail=request.form.get('detail') author_id = User.query.filter(User.username == session.get('user')).first().id wenda = Question(title=title,detail=detail,author_id=author_id) db.session.add(wenda) db.session.commit() return redirect(url_for('switch'))if __name__ == '__main__': app.run(debug=True)