博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
完成评论功能
阅读量:6319 次
发布时间:2019-06-22

本文共 4978 字,大约阅读时间需要 16 分钟。

    1. 定义评论的视图函数
      @app.route('/comment/',methods=['POST'])
      def comment():
      读取前端页面数据,保存到数据库中
    2. 用<input type="hidden" 方法获取前端的"question_id" 
    3. 显示评论次数
    4. 要求评论前登录
    5. 尝试实现详情页面下的评论列表显示
      from flask import Flask, render_template, request, redirect, url_for, sessionfrom flask_sqlalchemy import SQLAlchemyimport configfrom functools import wrapsfrom datetime import datetimeapp = 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)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'))    creat_time=db.Column(db.DateTime, default=datetime.now)    detail=db.Column(db.Text,nullable=False)    question=db.relationship('Question',backref=db.backref('comments'))    author=db.relationship('User',backref=db.backref('comments'))db.create_all()@app.route('/')def daohang():    context={        'questions':Question.query.order_by('creat_time').all()    }    return render_template('index.html',**context)@app.route('/detail/
      ',methods=['GET','POST'])def detail(question_id): quest=Question.query.filter(Question.id == question_id).first() return render_template('detail.html',ques=quest)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('/comment/',methods=['POST'])@loginFirstdef 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('detail',question_id=ques_id))@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, User.password == password).first() if user: session['user'] = username session.permanent = True return redirect(url_for('daohang')) else: return 'error username or password'@app.context_processordef mycontext(): usern = session.get('user') if usern: return {
      'username': usern} else: return {}@app.route('/logout/')def logout(): session.clear() return redirect(url_for('regist'))@app.route('/regist/', methods=['GET', 'POST'])def regist(): if request.method == 'GET': return render_template('regist.html') else: username = request.form.get('username') password = request.form.get('password') user = User.query.filter(User.username == username).first() if user: return 'username existed' else: user = User(username=username, password=password) db.session.add(user) db.session.commit() return redirect(url_for('login'))@app.route('/question/', methods=['GET', 'POST'])@loginFirstdef 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('daohang'))if __name__ == '__main__': app.run(debug=True)
      {% extends 'daohang.html' %}{
      % block title %}问答详情{% endblock %}{
      % block main %}

      detail{

      { ques.detail }}


      评论:({
      { ques.comments|length }})

      >
        >{
        % endblock %}

         

    转载于:https://www.cnblogs.com/chenhuafei/p/8004073.html

    你可能感兴趣的文章
    鸟哥学习笔记---DNS
    查看>>
    Linux 常用目录管理命令(cd pwd mkdir rmdir)
    查看>>
    java程序员菜鸟进阶(四)oracle基础详解(四)oracle开启和关闭服务程序——解决安装oracle占用大量内存...
    查看>>
    Flask_学习笔记_09: Flask中的继承
    查看>>
    Mahout源码目录说明
    查看>>
    我的友情链接
    查看>>
    Java学习日志(17-2-集合框架工具类Arrays及其他特性)
    查看>>
    HTTP响应头和请求头信息对照表
    查看>>
    Chrome完美屏蔽优酷广告及黑屏教程
    查看>>
    一份不错的php面试题(附答案)
    查看>>
    前端工程资源发布、优化
    查看>>
    nginx安装(ubuntu14.04)
    查看>>
    SQLServer2008备份和恢复
    查看>>
    WinCE 6.0 的编译
    查看>>
    访问Nginx上的资源时出现403的原因及解决办法
    查看>>
    大家好,我是蔡某某,刚刚注册的账号,希望大家支持与帮助
    查看>>
    shell检测输入的IP是否合法
    查看>>
    30 分钟快速入门 Docker 教程
    查看>>
    初步计划
    查看>>
    Ubuntu11.10下编译android源码4.0.3
    查看>>