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

1.主PY文件写视图函数,带id参数。 

@app.route('/detail/<question_id>') def detail(question_id):     quest =      return render_template('detail.html', ques = quest) 
@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.首页标题的标签做带参数的链接。       {{ url_for('detail',question_id = foo.id) }}

{% extends'base.html' %}
    {% block title %}
    首页
    {% endblock %}
{% block head %}

    <script src="{{url_for('static',filename='js/base.js') }}" type="text/javascript"></script>
    <link href="{{ url_for('static' ,filename='css/base.css') }}"  rel="stylesheet" type="text/css">
    {% endblock %}
    {% block main %}

    {% for foo in question %}
<body id="myBody">
        <div id="gufeng">
       <ul class="list" >
             <li>用户名:{{ foo.author.username }}</li>
          <a href="{{ url_for("detail",question_id=foo.id) }}">标题:{{ foo.title }}</a>
           <li class="">问题:{{ foo.detail }}</li>
           <li class="">时间:{{ foo.time }}</li>
       </ul>
        </div>
    {% endfor %}
    <footer>
        <div class="footer_box">
            Copyright@2017-2027 个人版权,版权所有 作者:JZX telephone:0000-1234567 mobile phone:11111111111
        </div>
    </footer>
{% endblock %}

 

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

{{ ques.title}} {{ ques.id  }}{{  ques.creat_time }}
{{ ques.author.username }}  {{ ques.detail }}
{% 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='talk'rows="10"id="detail"name="detail"></textarea>
        <br><input  type="submit" value="发布"style="width:100px;height:50px;font-size:50px">

        <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 %}

 

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

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

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'))

 

 5.尝试实现发布评论。

posted @ 2017-12-07 18:05  064曾志彬  阅读(167)  评论(0编辑  收藏  举报