• 评论列表显示及排序,个人中心显示


      1. 定义评论的视图函数
        @app.route('/comment/',methods=['POST'])
        def comment():
        读取前端页面数据,保存到数据库中
      2. 用<input type="hidden" 方法获取前端的"question_id" 
      3. 显示评论次数
      4. 要求评论前登录
      5. 尝试实现详情页面下的评论列表显示
          1.                                   
          2. py:
            from flask import Flask, render_template,request,redirect,url_for,session
            from flask_sqlalchemy import SQLAlchemy
            import config
            from functools import wraps
            from datetime import datetime
            
            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(100), nullable=False)
                password= db.Column(db.String(500), 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)
                creatTime = db.Column(db.DateTime,default=datetime.now)
                authorID = 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)
                creatTime = db.Column(db.DateTime,default=datetime.now)
                question = db.relationship('Question',backref=db.backref('comments',order_by=creatTime.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('/detail/<question_id>')
            def question_detail(question_id):
                question = Question.query.filter(Question.id == question_id).first()
                return render_template('detail.html',question=question)
            
            @app.route('/zhuce/',methods=['GET','POST'])
            def zhuce():
                if request.method == 'GET':
                    return render_template('zhuce.html')
                else:
                    username = request.form.get('username')  # 获取form中的数据
                    password = request.form.get('password')  # 获取form中的数据
                    email = request.form.get('email')  # 获取form中的数据
                    user = User.query.filter(User.username == username).first()
                    if user:
                        return'用户已存在'
                    else:
                        user = User(username = username,password=password)
                        db.session.add(user)
                        db.session.commit()
                        return redirect(url_for('denglu'))
            
            
            
            
            @app.route('/denglu/',methods=['GET','POST'])
            def denglu():
                if request.method=='GET':
                    return render_template('denglu.html')
                else:
                    username = request.form.get('username')
                    password = request.form.get('password')
            
                    user=User.query.filter(User.username==username).first()
            
                    if user:
                        session['user']=username
                        session.permanent=True
                        if user.password==password:
                            return redirect(url_for('shouye'))
                        else:
                            return '密码错误'
                    else:
                        return '此用户不存在'
            
            @app.context_processor
            def mycontext():
                usern=session.get('user')
                if usern:
                    return {'username':usern}
                else:
                    return{}
            
            @app.route('/logout/')
            def logout():
                session.clear()
                return redirect(url_for('shouye'))
            
            def login_re(func): #参数是函数
                @wraps(func)
                def wrapper(*args, ** kwargs): #定义个函数将其返回
                    if session.get('user'):
                        return func(*args,**kwargs)
                    else:
                        return redirect(url_for('denglu'))
                return wrapper #返回一个函数
            
            
            @app.route('/question/',methods=['GET','POST'])
            @login_re
            def question():
                if request.method == 'GET':
                    return render_template('question.html')
                else:
                    title = request.form.get('title')
                    detail = request.form.get('detail')
                    authorID = User.query.filter(User.username == session.get('user')).first().id
                    question = Question(title=title,detail=detail,authorID=authorID)
                    db.session.add(question)
                    db.session.commit()
                    return redirect(url_for(('shouye')))
            
            @app.route('/comment/',methods=['GET','POST'])
            @login_re
            def comment():
                detail = request.form.get('detail')
                author_id = User.query.filter(User.username == session.get('user')).first().id
                question_id = request.form.get('question_id')
                comment = Comment(author_id=author_id,question_id=question_id,detail=detail)
                db.session.add(comment)
                db.session.commit()
                return redirect(url_for('question_detail',question_id=question_id))
            
            
            @app.route('/self/<user_id>')
            def self(user_id):
                user = User.query.filter(User.id==user_id).first()
                context={
                    'user':user
            
                }
                return render_template('self.html',**context)
            
            if __name__ == '__main__':
                app.run(debug=True)

            html:

            {% extends'ba.html' %}
            {% block title %}
            
            {% endblock %}
            {% block head %}
                <link rel="stylesheet" href="{{ url_for('static',filename='css/self.css')}}" type="text/css">
            {% endblock %}
            {% block main %}
            <body>
            
            <div class="detail">
                <h2>{{ user.username }}</h2>
                <div class="all questions">
                    <p class="p">全部问答</p>
                    <div class="detail_left">
                        {% for foo in user.question %}
                            <span class="icon2" aria-hidden="true"></span>
                            <a href="#" class="name">{{ foo.author.username }}</a>
                            <span class="badge">{{ foo.creatTime }}</span>
                            <br>
                            <p class="title">{{ foo.title }}</p>
                            <p class="wenzhang" >{{ foo.detail }}</p>
                           <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div>
                        {% endfor %}
            
                    </div>
                </div>
            <div class="all comments">
                    <p class="p">全部评论</p>
                    <div class="detail_left">
                        {% for foo in user.comments %}
                            <span class="icon2" aria-hidden="true"></span>
                            <a href="#" class="name">{{ foo.author.username }}</a>
                            <span class="badge">{{ foo.creatTime }}</span>
                            <br>
                            <p class="neirong">{{ foo.detail }}</p>
                            <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div>
                        {% endfor %}
                    </div>
            </div>
            <div class="self">
                    <p class="p">个人信息</p>
                    <div class="detail_left">
                        <li>用户名:{{ user.username }}</li>
                        <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div>
                        <li>编号:{{ user.id }}</li>
                        <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div>
                        <li>文章篇数:{{ user.question|length }}</li>
                        <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div>
                    </div>
                </div>
            </div>
            </body>
            {% endblock %}

  • 相关阅读:
    mysql远程登录
    Linux下FTP服务器配置与管理
    linux编程的相对路径问题解决
    ERROR 1935_WIN7注册表大小的限制
    linux下jdk,tomcat的安装和配置
    使用Validator.validateAll验证Form
    mysql常用命令集合
    B or D
    Delphi开发日志系列文章的示例源码
    delphi开发日志——主窗体,程序管家
  • 原文地址:https://www.cnblogs.com/0055sun/p/8034517.html
Copyright © 2020-2023  润新知