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


    1. 显示所有评论
      {% for foo in ques.comments %}

    2. 所有评论排序
      uquestion = db.relationship('Question', backref=db.backref('comments', order_by=creat_time.desc))

    3. 显示评论条数
      {{ ques.comments|length }}

    4. 完成个人中心

    1.个人中心的页面布局(html文件及相应的样式文件)

    2.定义视图函数def usercenter(user_id):

    3.向前端页面传递参数

    4.页面显示相应数据

    发布的全部问答

    发布的全部评论

    个人信息

    5.各个页面链接到个人中心

    index.html 全部评论

    {% extends 'detail.html' %}
    {% block title %}首页{% endblock %}
    
    {% block main %}
        <img sr="{{ url_for('staic',filename='images/qalogo.png') }}" alt="qa">
        <ul class="list-group" style="....">
        {% for foo in question %}
            <li class="list-group-items">
            <span class="glyphicon glyphicon-leaf" aria-hidden="="true></span>
            <a href="{{ url_for('detail',question_id =foo.id) }}">{{ foo.title }}</a>
            <p style="">{{ foo.detail }}</p>
            <span class="glyphicon glyphicon-user" aria-hidden="true"></span>
            <a href="{{ url_for('usercenter',user_id=foo.author_id) }}">{{ foo.author.username }}评论:({{ foo.comments|length }})</a>
            <span class="badge">{{ foo.creat_time }}</span>
    
            </li>
        {% endfor %}
    
        </ul>
    
    {% endblock %}

    detail.html (全部问答)

    {% extends 'base.html' %}
    {%  block title %}问答详情{% endblock %}
    {% block main %}
    <div class="page-header">
        <h3>{{ ques.title }}<br><small>{{ ques.author.username }} <span class="badge">{{ ques.creat_time }}</span> </small></h3>
    
    </div>
    <p class="lead">{{ ques.detail }}</p>
    <hr>
    <form action="{{ url_for('comment') }}" method="post" style="..."...></form>
        <div class="form-group">
        <textarea name ="new_comment" class="form-control" rows="3"id="new-comment" placeholder="write your comment"></textarea>
        <input type="hidden" name="question_id" value="{{ ques.id }}">
        </div>
        <botton type="submit" class="btn btn-default">发送</botton>
        </form>
    
    <h4>评论:({{ ques.comments|length }}</h4>
    
    <ul class="list-group" style=""></ul>
        {% for foo in ques.comments %}
        <li class="list-group-item">
        <span class="glyphicon glyphicon-heart-empty" aria-hidden="true"></span>
        <a href="{{ url_for('usercenter',user_id=foo.author.id )}}">{{ foo.author.username }}</a>
            <span class="badge">{{ foo.create_time }}</span>
            <p style="align-content: center">{{ foo.detail }}</p>
        </li>
    {%  endfor %}
    </ul>

    usercenter

    {% extends 'base.html' %}
    {% block title %}个人中心{% endblock %}
    {% block main %}
    
        <div class="page-header">
        <h3><span class="glyphicon glyphicon-user" aria-hidden="true"></span>{{ username }}<br><small></small> 全部问答<span class="badge"></span> </h3>
        <ul class="list-group"style="align-content: center">
            {% for foo in question %}
            <li class="list-group-item">
                <span class="glyphicon glyphicon-heart-empty" aria-hidden="true"></span>
                <a href="#">{{ foo.author.username }}</a>
                <span class="badge">{{ foo.creat_time }}</span>
                <p style="align-content: center">{{ foo.detail }}</p>
    
            </li>
            {% endblock %}
        </ul>
        </div>
    
    
    
    <div class="page-header">
        <h3><span class="glyphicon glyphicon-user" aria-hidden="true"></span>{{ user }}<br><small>全部评论<span class="badge"></span> </small> </h3>
        <ul class="list-group"style="align-content: center">
            {% for foo in user.comments %}
            <li class="list-group-item">
                <span class="glyphicon glyphicon-heart-empty" aria-hidden="true"></span>
                <span class="badge">{{ foo.creat_time }}</span>
                <p style="align-content: center">{{ foo.detail }}</p>
            </li>
            {% endfor %}
        </ul>
    </div>
    
    
    
    <div class="panel-header">
        <h3><span class="glyphicon glyphicon-user" aria-hidden="true"></span>{{ user }}<br><small>个人信息<span class="badge"></span> </small> </h3>
        <ul class="list-group"style="align-content: center">
            <li class="list-group-item">用户:{{ username }}</li>
            <li class="list-group-item">编号:</li>
            <li class="list-group-item">昵称:</li>
            <li class="list-group-item">文章篇数:</li>
        </ul>
    </div>
    {% endblock %}

    base.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
            <li class="active"><a href="{{ url_for('index') }}">首页<span class="sr-only"></span> </a> </li>
            <li><a href="{{ url_for('question') }}">发布问答</a> </li>
        </ul>
        <form class="navbar-form navbar-left">
            <div class="form-group">
                <input type="texte" class="form-control" placeholder="请输入关键词">
    
            </div>
            <botton type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search" aria-hidden="true"></span> 炸找 </botton>
    
        </form>
        <ul class="nav navbar-nav navbar-right">
            {% if user %}
                <li><a href="{{ url_for('usercenter',user_id=session.get('userid))}}">{{ session.get('user') }}</a> </li>
                <li><a href="{{  }}"
    
            {% endif %}
        </ul>
    </div>
    
    </body>
    </html>

    py文件

    from flask import Flask,render_template,request,redirect,url_for,session
    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    import config
    from functools import wraps
    from datetime import datetime
    
    app = Flask(__name__)
    #app.config['SECRET_KEY'] = '1234567'
    app.config.from_object(config)
    db = SQLAlchemy(app)
    
    class User(db.Model):
        __tablename__ = 'user_test'
        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(20))
        tel = db.Column(db.String(20))
    
    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_test.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('comment'))
    
    
    
    #db.create_all()
    
    
    #user = User(username = 'mis1',password = '124', nickname='wk')
    #db.session.add(user)
    #db.session.commit()
    
    
    
    #user = User.query.filter(User.username == 'mis1234').first()
    #print(user)
    #print(user.id,user.username)
    
    
    #user = User.query.filter(User.username == 'mis1234').first()
    #user.password = '123'
    #db.session.commit()
    
    
    # user = User(username = 'wkkkkkk',password = '123456', nickname='kk')
    # db.session.add(user)
    # db.session.commit()
    
    
    #user = User.query.filter(User.username == 'mis1234').first()
    #db.session.delete(user)
    #db.session.commit()
    
    @app.route()
    
    
    @app.route('/')
    def index():
        context={
            'questions':Question.query.all()
        }
        return render_template('index.html',**context)
    
    
    @app.route('/detail/<question_id>')
    def detail(question_id):
        return render_template('detail.html',ques = question_id)
    
    
    
    
    
    
    
    @app.route('/login/',methods=['GET','POST'])
    def login():
        if request.method == 'GET':
            return render_template('login.html')
        else:
            usern = request.form.get('uname')
            passw = request.form.get('upass')
            user = User.query.filter(User.username==usern).first()
            if user:
                if user.password == passw:
                    session['user'] = usern;
                    return redirect(url_for('index'))
                else:
                    return 'password error!'
            else:
                return 'username is not exister!'
    
    
    
    @app.route('/register/',methods=['GET','POST'])
    def register():
        if request.method =='GET':
            return  render_template('register.html')
        else:
            usern = request.form.get('uname')
            teln = request.form.get('number')
            passw = request.form.get('upass')
            user = User.query.filter(User.username==usern).first()
            if user:
                return 'username existed'
            else:
                user1= User(username=usern,tel=teln,password=passw)
                db.session.add(user1)
                db.session.commit()
                return redirect(url_for('login'))
    
    
    @app.context_processor
    def mycontext():
        usern = session.get('user')
        if usern:
            return{'username':usern}
        else:
            return {}
    
    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('/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
            author_id=user.id
            question = Question(title=title,detail=detail,author_id=author_id)
            question.author=user
    
            db.session.add(question)
            db.session.commit()
        return redirect(url_for('index'))
    
    @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('/comment/',methods=['POST'])
    def comment():
        comment=request.form.get('nem_comment')
        ques_id=request.form.get('question_id')
        auth_id=User.query.filter(User.username==session.get('user')).first().id
        comm=Comme(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('/usercenter/<user_id>')
    @login_re
    def usercenter(user_id):
        user=User.query.filter(User.id==user_id).first()
        context={
            'user':user
        }
        return render_template('usercenter.html',**context)
    
    
    
    if __name__ == '__main__':
        app.run(debug=True)

     

  • 相关阅读:
    Excel的VBA小练习
    敏捷和产品
    Ubuntu的系统应用
    也晒晒生产力工具:键盘
    SQLServer 2012 Ent 安装失败,另辟蹊径
    还是要精简开发呀,VS2015太大,VS2010不想装
    必须夸夸Sublime,大文件打开
    过节了,开源中国歪歪了!!!
    关于VisualStudio2010发布项目问题
    Visual Studio 各个版本汇总
  • 原文地址:https://www.cnblogs.com/123hyf/p/8027126.html
Copyright © 2020-2023  润新知