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

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

@app.route('/detail/<question_id>')
def detail(question_id):
    quest = 
    return render_template('detail.html', ques = quest) 
def detailpage(question_id):
    quest=Question.query.filter(Question.id==question_id).first()

    return render_template('detailpage.html',ques=quest)

 

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

    {{ url_for('detail',question_id = foo.id) }}

<ul class="list-group" style="margin-left:20%;margin-top: 5%;margin-right:20%">
{#    <li class="list-group-item">#}
{#        <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>#}
{#        <a href="{{ url_for('detail')}}">username</a>#}
{#        <br>#}
{#        <a href="#)"title></a>#}
{#        <span class="badge">create_time</span>#}
{#       <p>detauk</p>#}
        {% for b in question %}
                <li class="list-group-item" >
                    <a href={{ url_for('detail')}}>{{ b.author.username }}</a>
                    <span class="badge">{{ b.creat_time }}</span>
                    <p class="abstract">{{ b.detail }}</p>
                </li>
        {% endfor %}
    </ul>

 

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

{{ ques.title}}
{{ ques.id  }}{{  ques.creat_time }}
{{ ques.author.username }} 
{{ ques.detail }}
<div align="center">
        <div class="a1">
             <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">
            <h4>评论:({{ ques.comments|lenght }})</h4>
        </form>
{#                    <h2>Beyond乐队</h2>#}
{#                    <p>Beyond</p>#}
{#               <p>1983年成立</p>#}
{#                   <p>detail</p>#}
            <br></div>
        <div class="a2">
            <div class="pinglun"><label for="comment">评论</label><br></div>
            <textarea id="comment" rows="12" name="commentdetail"></textarea></div>
        <input type="submit" class="fasong" value="发送评论">
    </div>

 

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

class Comment(db.Model):

    __tablename__='comment'

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

 

5、 尝试实现发布评论。

posted @ 2017-12-07 13:13  070王学竞  阅读(122)  评论(0编辑  收藏  举报