从首页问答标题到问答详情页
- 主PY文件写视图函数,带id参数。
@app.route('/detail/<question_id>')
def detail(question_id):
quest =
return render_template('detail.html', ques = quest) - 首页标题的标签做带参数的链接。
{{ url_for('detail',question_id = foo.id) }} - 在详情页将数据的显示在恰当的位置。
{{ ques.title}}
{{ ques.id }}{{ ques.creat_time }}{{ ques.author.username }}
{{ ques.detail }} -
建立评论的对象关系映射:
class Comment(db.Model):
__tablename__='comment' -
尝试实现发布评论。
@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)
{% extends'base.html' %} {% block title %} 首页 {% endblock %} {% block head %} <link rel="stylesheet" href="{{ url_for('static',filename='css/shouye.css')}}" type="text/css"> {% endblock %} {% block main %} <body> <div class="content"> <p class="biaoti">发布的内容</p> <ul class="list-group"> {% for foo in question %} <li class="list-group-item"> <span class="glyphicon-leaf" aria-hidden="true"></span> <img id="qu" src="{{ url_for('static',filename='images/title.png')}}"> <a href = "{{url_for('detail',question_id=foo.id)}}">{{ foo.title }}</a> <p style="text-align: left">{{ foo.detail }}</p> <img id="qu" src="{{ url_for('static',filename='images/question.jpg')}}"> <a href = "">{{ foo.author.username }}</a> <br> <span class="badge" style="float: right;background-color: burlywood;border-radius: 5px">{{ foo.create_time }}</span> </li> {% endfor %} </ul> </div> </body> {% endblock %}
{% extends'base.html' %} {% block title %} 问答详情 {% endblock %} {% block head %} <link rel="stylesheet" href="{{ url_for('static',filename='css/detail.css')}}" type="text/css"> {% endblock %} {% block main %} <div class="content"> <h3>标题:{{ ques.title }}<br><br><small>作者:{{ ques.author.username }} <br> <span class="badge">发布时间:{{ ques.create_time }}</span> </small></h3> <hr> <p class="lead">内容:{{ ques.detail }}</p> <hr> </div> {# <form action="{{ url_for('/detail/<question_id>') }}" method="post">#} <textarea class="comment" rows="8" name="title" placeholder="写下你的评论:" name="comment"></textarea> <button class="button">发布 </button> <span class="tcomment">评论:({{ ques.comments|length }})</span> <ul class="list-group"> {% for foo in question %} <li class="list-group-item"> <span class="glyphicon-leaf" aria-hidden="true"></span> <img id="qu" src="{{ url_for('static',filename='images/title.png')}}"> <a href = "#">{{ foo.author.username }}{{ foo.title }}</a> <span class="badge">{{ foo.create_time }}</span> </li> {% endfor %} </ul> {% endblock %}
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) create_time = db.Column(db.DateTime, default=datetime.now) question = db.relationship('Question', backref=db.backref('comments',order_by=create_time.desc)) author = db.relationship('User', backref=db.backref('comments'))