- 首页列表显示全部问答:
- 将数据库查询结果传递到前端页面 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 app = Flask(__name__) app.config.from_object(config) 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(50)) class Question(db.Model): __tablename__ = 'question' id = db.Column(db.Integer,primary_key=True,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('question')) db.create_all() @app.route('/') def shouye(): context = { 'questions': Question.query.all() } return render_template('shouye.html', **context) @app.route('/denglu/',methods=['GET','POST']) def gg(): if request.method == 'GET': return render_template('denglu.html') #跳转登录 else: usern = request.form.get('username') passw = request.form.get('password') user = User.query.filter(User.username == usern).first() if user: if user.password == passw: session['user'] = usern session.parmanent = True return redirect(url_for('shouye')) else: return u'password error' else: return u'username is not existed' @app.route('/zhuche/', methods=['GET','POST']) def login (): if request.method =='GET': return render_template('zhuce.html')#跳转注册 else: usern = request.form.get('username') passw = request.form.get('password') nickn = request.form.get('nickname') user = User.query.filter(User.username == usern).first() if user: return u'username existed' else: user1 = User(username = usern,password = passw,nickname=nickn) db.session.add(user1)#数据库操作 db.session.commit() return redirect(url_for('gg')) @app.route('/tupian/') def hh(): return render_template('tupian.html') #跳转图片库 def loginFirst(func): @wraps(func) def wrapper (*args,**kwargs): if session.get('user'): return func(*args,**kwargs) else: return redirect(url_for('gg')) return wrapper @app.route('/fabu/',methods=['GET','POST']) @loginFirst def fabu(): if request.method == 'GET': return render_template('fabu.html') else: title = request.form.get('title') detail = request.form.get('detail') author_id = User.query.filter(User.username == session.get('user')).first().id question = Question(title=title, detail=detail, author_id=author_id) db.session.add(question) db.session.commit() return redirect(url_for('shouye')) # 跳转发布 @app.context_processor def context(): usern = session.get('user') if usern: return {'username': usern } else: return {} @app.route('/logout/') def logout(): session.clear() return redirect(url_for('shouye')) if __name__ == '__main__': app.run(debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>详细发布</title> </head> <body> <div class="box"></div> <div class="box1 "> <ul class="box2"> <li> <h2 href="#" class="title">title</h2> <hr> <p class="user"> <a href="#"> <small>username</small></a> <span class="time"><small>create_time</small></span> </p> <hr> <form> <div class="pinglun"> <textarea name="comment" class="form-control" cols="50" rows="5" id="comment" placeholder="请输入评论"></textarea> </div> <button type="submit" class="button">发送</button> </form> </li> </ul> </div> </body> </html>