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

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

@app.route('/detail/<question_id>')
def detail(question_id):
    quest = 
    return render_template('detail.html', ques = quest) 

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


@app.route('/comment/',methods=['POST'])
@loginFrist
def comment():
    comment = request.form.get('new_comment')
    ques_id= request.form.get('question_id')
    auth_id=User.query.filter(User.username == session.get('User')).first().id
    comm =Comment(author_id=auth_id,question_id=ques_id,detail=comment)
    db.session.add(comm)
    db.session.comment()

    return credits(url_for('detail',question_id=ques_id))

 

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'

{% extends 'basic.html' %}
{% block title %}detail{% endblock %}

{% block head %}
    <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='../staticcss/20.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='../staticcss/200.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='../static/css/2000.css') }}">
    <script src="{{url_for('static',filename='../static/js/39.js') }}" type="text/css"></script>
{% endblock %}

{% block detail %}
<body style="margin:0" align="center" >
<div>
    <h3>{{ ques.title }}<br><small>{{ ques.author.username }}<span>{{ ques.creat_time }}
    </span>
    </small></h3>
</div>
<p>{{ ques.detail }}</p>
<hr>
<from action="{{ url_for('comment') }}"method="post"style="">
    <div>
        <textarea name="new_comment"class="form-control"rows="3"id="new-comment"placeholder="write your comment"></textarea>

    </div>
    <button type="submit" >send</button>
</from>
<h4>comment:({{ ques.commemts|length }})</h4>

</body>





{% endblock %}

 

posted @ 2017-12-07 19:27  067杜嘉成  阅读(97)  评论(0编辑  收藏  举报