@app.route('/detail/<question_id>')
def detail(question_id):
quest =
return render_template('detail.html', ques = quest)
{{ ques.title}}
{{ ques.id }}{{ ques.creat_time }}
{{ ques.author.username }}
{{ ques.detail }}
一、主PY文件寫視圖函數,帶id參數。 css
@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)
二、首頁標題的標籤作帶參數的連接。
{{ url_for('detail',question_id = foo.id) }}html
shouye.html:bootstrap
{% 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 %}
三、在詳情頁將數據的顯示在恰當的位置。 app
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 %}
結果:url
四、創建評論的對象關係映射:spa
class Comment(db.Model):code
__tablename__='comment'orm
五、嘗試:cdn
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'))