从首页问答标题到问答详情页

Posted J芷璇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从首页问答标题到问答详情页相关的知识,希望对你有一定的参考价值。

    1. 主PY文件写视图函数,带id参数。 
      @app.route(‘/detail/<question_id>‘)
      def detail(question_id):
          quest = 
          return render_template(‘detail.html‘, ques = quest)
    2. 首页标题的标签做带参数的链接。
            {{ url_for(‘detail‘,question_id = foo.id) }}

    3. 在详情页将数据的显示在恰当的位置。 
      {{ ques.title}}
      {{ ques.id  }}{{  ques.creat_time }}
      {{ ques.author.username }} 
      {{ ques.detail }}
    4. 建立评论的对象关系映射:

      class Comment(db.Model):
          __tablename__=‘comment‘

    5.  尝试实现发布评论。

 1.主PY文件写视图函数,带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)

2.首页标题的标签做带参数的链接。

{% extends‘base.html‘ %}
{% block title %}
    首页
{% endblock %}
{% block head %}
    <link rel="stylesheet" href="{{ url_for(‘static‘,filename=‘css/index.css‘) }}" type="text/css">
{% endblock %}
{% block main %}
    <body id="myBody">
    <div class="indexone">
        <ul class="list-group">
            {% for foo in questions %}
                <li class="list-group-item">
                    <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
                     <a href="#" style="font-family: 幼圆">{{ foo.author.username }}</a><br>
                    <a href="{{ url_for(‘detail‘,question_id = foo.id) }}" style="font-family: 幼圆">{{ foo.title }}</a><br>
                    <span class="badge" style="font-family: 幼圆">{{ foo.creat_time }}</span><br>
                    <p style="font-family: 幼圆;color: #002D54;">{{ foo.detail }}</p>
                    <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
                <div class="meta">
                            <a class="collection-tag">
                                <span style="font-family: 幼圆">社会热点</span>
                            </a>
                            <a class="collection-tag">
                                <span style="font-family: 幼圆"> 浏览: </span><span
                                    style="font-family: 幼圆;color: red"> 555 </span>
                            </a>
                            <a class="collection-tag">
                                <span style="font-family: 幼圆"> 评论: </span><span
                                    style="font-family: 幼圆;color: red"> 890 </span>
                            </a>
                            <span style="font-family: 幼圆"> 点赞: </span><span
                                style="font-family: 幼圆;color: red"> 1234 </span>
                        </div>
                </li>
            {% endfor %}
        </ul>
    </div>

    </body>
    </html>
{% endblock %}

 

3.在详情页将数据的显示在恰当的位置。 

{% extends‘base.html‘ %}
{% block title %}
    问答详情
{% endblock %}
{% block head %}
    <link rel="stylesheet" href="{{ url_for(‘static‘,filename=‘css/detail.css‘) }}" type="text/css">
{% endblock %}
{% block main %}
    <body id="myBody">
    <div class="total">
        <div class="page-header" style="text-align: left">
            <h3>{{ ques.title }}<br>
                <small>作者:{{ ques.author.username }}&nbsp&nbsp&nbsp<span class="badge">{{ ques.creat_time }}</span>
                </small>
            </h3>
        </div>
        <hr>
        <p class="lead">{{ ques.detail }}</p>
        <hr>
        <form method="post" action="{{ url_for(‘comment‘) }}">
            <div class="title" style="text-align: left">
                <textarea class="form-control" rows="3" placeholder="请输入要发布的内容" name="detail"></textarea>
            </div>
            <button type="submit" id="fabu">发送</button>
            <h3 style="text-align: left">评论:({{ ques.comments|length }})</h3>
            <div class="basic_box" style="padding-bottom: 50px;">
                <ul class="list-group" style="margin-bottom: 10px">
                    {% for foo in comments %}
                        <li class="list-group-item" style="width: 800px">
                            <span class="glyphicon glyphicon-left" aria-hidden="true"></span>
                            <br>
                            <a href="#">{{ foo.author_id }}</a>
                            <span class="badge">评论时间:{{ foo.creat_time }}</span>
                            <p>{{ foo.detail }}
                            </p>
                        </li>
                    {% endfor %}
                 </ul>
            </div>
        </form>
    </div>
    </body>
    </html>
{% endblock %}

4.建立评论的对象关系映射:

 5.尝试实现发布评论。

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))
    author = db.relationship(User, backref=db.backref(comments))

以上是关于从首页问答标题到问答详情页的主要内容,如果未能解决你的问题,请参考以下文章

从首页问答标题到问答详情页

从首页问答标题到问答详情页

从首页问答标题到问答详情页

从首页问答标题到问答详情页

从首页问答标题到问答详情页

从首页问答标题到问答详情页