代码改变世界

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

2017-12-07 11:45  095罗其婷  阅读(111)  评论(0)    收藏  举报

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):
    Question.query.filter(Question.id == question_id)

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

 

2、首页标题的标签做带参数的链接。
      {{ url_for('detail',question_id = foo.id) }}

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


{% block main %}
    <img src="{{ url_for('static',filename='images/jing.jpg') }}" alt="ji">
    <p>{{ user }}context</p>


    <ul class="list-group" style="...">
        {% for foo in question %}
            <li class="list-group-item">
                <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
                <a href="#">{{ url_for('detail',question_id=foo.id)}}">{{foo.title }}</a>
                <p style="text-indent: 18px">{{ foo.detail }}</p>
                <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
                <a href="{{ url_for('usercenter',user_id=foo.author_id)}}">{{foo.author.username }}评论:({{ foo.comments|length }}</a><br>

                <span class="badge">{{ foo.creat_time }}</span>

            </li>

        </ul>



{% endblock %}

 

 

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

{{ ques.title}}
{{ ques.id  }}{{  ques.creat_time }}
{{ ques.author.username }} 
{{ ques.detail }}
{% extends 'base.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="width: 30px" src="{{ url_for('static',filename='css/pink.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 %}

 

 

4、建立评论的对象关系映射:
class Comment(db.Model):
    __tablename__='comment'

 

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