- 定义评论的视图函数
@app.route('/comment/',methods=['POST'])
def comment():
读取前端页面数据,保存到数据库中 - 用<input type="hidden" 方法获取前端的"question_id"
- 显示评论次数
- 要求评论前登录
- 尝试实现详情页面下的评论列表显示
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')) class Comment(db.Model): __tablename__='comment' id = db.Column(db.Integer, primary_key=True, autoincrement=True) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) question_id = db.Column(db.Integer, db.ForeignKey('question.id')) detail = db.Column(db.Text, nullable=False) creat_time = db.Column(db.DateTime, default=datetime.now) question = db.relationship('Question', backref=db.backref('comments',order_by=creat_time.desc)) author = db.relationship('User', backref=db.backref('comments')) 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('/xiangqing/<question_id>') def xiangqing(question_id): quest = Question.query.filter(Question.id == question_id).first() return render_template('xiangqing.html',ques = quest) @app.route('/comment',methods=['POST']) @loginFirst def comment(): comment =request.form.get('new_comment') ques_id =request.form.get('question_id') auth_id =User.query.filter(User.username == session.get('user')).first().id comm = Comment(author_id=auth_id,question_id=ques_id,detail=comment) db.session.add(comm) db.session.commit() return redirect(url_for('xiangqing',question_id=ques_id)) @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)
{% extends 'base.html' %} {% block xiangqingtitle %}详细问答{% endblock %} {% block xiangqinghead %} <link href="../static/css/xiangqing.css" rel="stylesheet" type="text/css"> {% endblock %} {% block xiangqingbody %} <div class="card" style="width:50rem; margin:0 auto"> <h1>详细问答</h1> <li class="list-group-item" > <span class="icon" aria-hidden="true"></span> <br> <a href="#">标题:{{ ques.title }}</a> <br> <a href="#">用户名:{{ ques.author.username}}</a> <span class="badge"style="float:right">{{ ques.creat_time}}</span> </li> <li class="list-group-item" style="height: 10rem" > <p>{{ ques.detail }}</p> </li> <form action="{{ url_for('comment') }}"method="post" > <div class="form-group"> <textarea name="new_comment" class="form-control" rows="5" id="comment" placeholder="请输入评论"></textarea> <input name="question_id" type="hidden" value="{{ ques.id }}"/> </div> <button type="submit" >发送</button> </form> <li class="list-group-item"style="height:3rem">最新评论:({{ ques.comments|length }})</li> <ul class="list-group"style="height: 50rem"> {% for foo in ques.comments %} <li class="list-group-item"> <span class="glyphicon" glyphicon-leaf aria-hidden="true"></span> <a href="#">用户名:{{ foo.author.username }}</a> <br> <a href="{{ url_for('xiangqing',question_id = foo.id) }}">标题:{{ foo.title }}</a> <span class="badge">{{ foo.creat_time }}</span> <br> <p class="neirong">内容:{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> {% endblock %}