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

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

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

首页代码

{% extends 'ba.html' %}
{% block title %}
    首页
{% endblock %}
{% block main %}
    <link rel="stylesheet" type="text/css" href="../static/css/wenda.css">
    <p style="margin-left: 15%">欢迎您:{{ username }}</p>
    <div class="box">
        <ul class="list-group">
            {% for foo in questions %}
                <li class="list-group-item">
                    <img style=" 30px" src="{{ url_for('static',filename='css/touxiang.jpg') }}" alt="64">
                    <a href="#">{{ username }}</a><br>
                    <a href="{{ url_for('detail',question_id=foo.id) }}">问题:{{ foo.title }}</a><br>
                    <p style="align-content: center">{{ foo.detail }}</p>
                    <span class="badge" style="margin-left: 60%">{{ foo.create_time }}</span>
                    <p style="margin-left: 25%"></p><br>
                </li>
            {% endfor %}
        </ul>
    </div>
{% endblock %}

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

{{ ques.title}}
{{ ques.id  }}{{  ques.creat_time }}
{{ ques.author.username }} 
{{ ques.detail }}
{% extends 'ba.html' %}
{% block title %}
    问答详情
{% endblock %}
{% block main %}
    <div class="box">
        <h3 href="#" >{{ ques.title }}</h3><small> {{ ques.author.username }}<span class="badge" style="margin-left: 75%">{{ ques.create_time }}</span></small>
        <hr>
        <p>{{ ques.detail }}</p>
        <hr>
        <form>
            <div><textarea class="form-control" id="comment" rows="3" style="margin-left: 1%" name="comment" placeholder="write your comment"></textarea><br></div>
            <button type="submit" >发送</button>
        </form>
      <h4>评论:</h4>
         <ul class="list-group">
                <li class="list-group-item">
                    <img style=" 30px" src="{{ url_for('static',filename='css/touxiang.jpg') }}" alt="64">
                    <a href="#"></a><br>
                    <p style="align-content: center"></p>
                    <span class="badge" style="margin-left: 60%"></span>
                    <p style="margin-left: 25%"></p><br>
                </li>
        </ul>

    </div>
{% endblock %}
  1. 建立评论的对象关系映射:

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

2. 尝试实现发布评论。

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'))
     create_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')
原文地址:https://www.cnblogs.com/cch-1007/p/7989116.html