- 首页列表显示全部问答:
- 将数据库查询结果传递到前端页面 Question.query.all()
- 前端页面循环显示整个列表。
- 问答排序
- 完成问答详情页布局:
- 包含问答的全部信息
- 评论区
- 以往评论列表显示区。
- 在首页点击问答标题,链接到相应详情页。
from flask import Flask,render_template,request,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy from functools import wraps from datetime import datetime import config,os app=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/') 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_processor def 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']) @loginFirst def 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)