从首页问答标题到问答详情页(2017.12.5)
- 主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 }}
1、主PY文件写视图函数,带id参数。
@app.route('/xiangqing/<question_id>') def xiangqing(question_id): quest=Question.query.filter(Question.id==question_id).first() return render_template('xiangqing.html',ques=quest)
2、首页标题的标签做带参数的链接。
{{ url_for('detail',question_id = foo.id) }}
shouye.html:
{% extends 'base.html' %}
{% block title %}首页{% endblock %}
{% block head %}
<link rel="stylesheet" type="text/css" href="../static/css/shouye.css">
{% endblock %}
{% block main %}
<div class="question-box">
<P style="font-size: 18px">{{ user }}prefect</P>
<ul class="list-group" style=" padding-left: 10px;padding-right: 10px;">
{% for foo in questions %}
<li class="list-group-item"
style="padding-left: 0px; padding-right: 10px; box-shadow: rgba(0, 0, 0, 0.498039) 0px 0px 0px 0px;">
<img src=http://p1.so.qhimgs1.com/t01e53e241b251ff5e1.jpg alt="64">
<span class="glyphicon glyhicon-leaf" aria-hidden="true"></span>
<a href="#">呆梨:{{ foo.username }}</a><br>
<a href="{{ url_for('xiangqing',question_id=foo.id )}}">问题:{{ foo.title }}</a><br>
<p style="color: indianred">详情:{{ foo.detail }}</p>
<span class="badge">首播时间:{{ foo.creat_time }}</span><br>
</li>
{% endfor %}
</ul>
</div>
{% endblock %}
3、在详情页将数据的显示在恰当的位置。
index.html:
{% extends 'base.html' %}
{% block title %}问答详情{% endblock %}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
{% block main %}
<div class="col-md-2 column "></div>
<div class="col-md-8 column ">
<div class="page-headr">
<h1>welcome to here
!</h1><br>
<h3>Title{{ ques.title }}<br>
<small>author:{{ ques.author.username }}<span class="badge">time:{{ ques.creat_time }}</span></small>
</h3>
<hr>
<p>detail:{{ ques.detail }}</p>
<hr>
<form>
<div class="form-group">
<textarea name="new_comment" class="form-control" rows="5" id="new-comment"
placeholder="Write your comment~" style="width: 850px"></textarea><br>
</div>
<button type="submit" class="btn btn-default" style="width:100px "> 发送
</button>
</form>
{# <h4>comment:({{ ques.comments|length }})</h4>#}
<ul class="list-group" style="margin: 10px"></ul>
</div>
</div>
<div class="col-md-2 column "></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')) creat_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'))

浙公网安备 33010602011771号