1.主PY文件寫視圖函數,帶id參數。 javascript
@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): quest = Question.query.filter(Question.id == question_id).first() return render_template('detail.html',ques=quest)
2.首頁標題的標籤作帶參數的連接。 {{ url_for('detail',question_id = foo.id) }}css
{% extends'base.html' %} {% block title %} 首頁 {% endblock %} {% block head %} <script src="{{url_for('static',filename='js/base.js') }}" type="text/javascript"></script> <link href="{{ url_for('static' ,filename='css/base.css') }}" rel="stylesheet" type="text/css"> {% endblock %} {% block main %} {% for foo in question %} <body id="myBody"> <div id="gufeng"> <ul class="list" > <li>用戶名:{{ foo.author.username }}</li> <a href="{{ url_for("detail",question_id=foo.id) }}">標題:{{ foo.title }}</a> <li class="">問題:{{ foo.detail }}</li> <li class="">時間:{{ foo.time }}</li> </ul> </div> {% endfor %} <footer> <div class="footer_box"> Copyright@2017-2027 我的版權,版權全部 做者:JZX telephone:0000-1234567 mobile phone:11111111111 </div> </footer> {% endblock %}
3.在詳情頁將數據的顯示在恰當的位置。 html
{{ ques.title}} {{ ques.id }}{{ ques.creat_time }}
{{ ques.author.username }} {{ ques.detail }}
{% extends "base.html" %} {% block title%}詳細內容{% endblock %} {% block main %} <body bgcolor="#ffd700"> <div class="GuFeng"> { <form action="{{ url_for('/detail/<question_id>') }}" method="post">} <h2>標題:{{ ques.title }}</h2><br> <h3>用戶名:{{ ques.author.username }}</h3><br> <h3> 發佈時間:{{ ques.create_time }}</h3> <p>內容:{{ ques.detail }}</p> <hr> <textarea class='talk'rows="10"id="detail"name="detail"></textarea> <br><input type="submit" value="發佈"style="width:100px;height:50px;font-size:50px"> <p>評論:</p> <table border=5 style="background: gold"width="50"> <tr><td>評論內容</td></tr> <tr><td>評論內容</td> </tr> <tr><td>評論內容</td></tr> </tr> </table> </div> </body> {% endblock %}
4.創建評論的對象關係映射:java
class Comment(db.Model): __tablename__='comment'app
class Comment(db.Model): __tablename__ = 'commment' 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) time = db.Column(db.DateTime, default=datetime.now) question=db.relationship('Question',backref=db.backref('comments')) author=db.relationship('User',backref=db.backref('comments'))
5.嘗試實現發佈評論。函數