完成评论功能
1.定义评论的视图函数 @app.route('/comment/',methods=['POST']) def comment(): 读取前端页面数据,保存到数据库中
2.用<input type="hidden" 方法获取前端的"question_id"
3.显示评论次数
4.要求评论前登录
5.尝试实现详情页面下的评论列表显示
py
from flask import Flask,render_template,request,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy import config from datetime import datetime from functools import wraps app = Flask(__name__) app.config.from_object(config) db=SQLAlchemy(app) class User(db.Model): __table__name = '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() class Comment(db.Model): __tablename__ = 'commment' 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) time = db.Column(db.DateTime, default=datetime.now) question=db.relationship('Question',backref=db.backref('comments')) author=db.relationship('User',backref=db.backref('comments')) @app.route('/',methods=['GET','POST']) def index(): context={ 'question': Question.query.all() } return render_template('index.html',**context) @app.route('/question/',methods=['GET','POST']) @loginFirst def question(): if request.method == 'GET': return render_template('question.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('index')) @app.route('/comment/',methods=['POST']) def comment(): comment = request.form.get('detail') 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('detail',question_id=ques_id)) @app.route('/detail/<question_id>') def detail(question_id): quest = Question.query.filter(Question.id == question_id).first() return render_template('detail.html',ques=quest) @app.route('/zhuce/',methods=['GET','POST']) def zhuce(): if request.method =='GET': return render_template('zhuce.html') else: usern = request.form.get('name') teln = request.form.get('password') passw = request.form.get('password1') user = User.query.filter(User.username==usern).first() if user: return 'username existed' else: user= User(username=usern,tel=teln,password=passw) db.session.add(user) db.session.commit() return redirect(url_for('login')) @app.route('/login/', methods=['GET','POST']) def login(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('username') password = request.form.get('password') user = User.query.filter(User.username == username).first() if user: if user.password ==password: session['user'] = username return redirect(url_for('index')) else: return u'password error' else: return u'username is not existed' @app.route('/logout/') def logout(): session.clear() return redirect(url_for('index')) 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.context_processor def mycontext(): usern = session.get('user') if usern: return{'username':usern} else: return {} if __name__ == '__main__': app.run(debug=True)
html
{% extends "base.html" %} {% block title%}详细内容{% endblock %} {% block main %} <body bgcolor="#ffd700"> <div class="GuFeng"> { <form action="{{ url_for('/detail/<question_id>') }}" method="post">} <h2>标题:{{ ques.title }}</h2><br> <h3>用户名:{{ ques.author.username }}</h3><br> <h3> 发布时间:{{ ques.create_time }}</h3> <p>内容:{{ ques.detail }}</p> <hr> <textarea class='comment'rows="10"id="detail"name="detail"></textarea> <br><input type="submit" value="发布"style="width:100px;height:50px;font-size:50px"> <input name="question_id" type="hidden" value="{{ ques.id }}"/> <p>评论:</p> <table border=5 style="background: gold"width="50"> <tr><td>评论内容</td></tr> <tr><td>评论内容</td> </tr> <tr><td>评论内容</td></tr> </tr> </table> </div> </body> {% endblock %}