1. 程式人生 > >從首頁問答標題到問答詳情頁

從首頁問答標題到問答詳情頁

username have route model share styles als int 參數

主PY文件寫視圖函數,帶id參數。

@app.route(‘/detail/<question_id>‘)
def detail(question_id):
quest =
return render_template(‘detail.html‘, ques = quest)
@app.route(‘/detail/<sent_id>‘)
def detail(sent_id):
    sentt = Sent.query.filter(Sent.id == sent_id).first()
    return render_template(‘detail.html‘, sen=sentt)

首頁標題的標簽做帶參數的鏈接。
{{ url_for(‘detail‘,question_id = foo.id) }}

在詳情頁將數據的顯示在恰當的位置。

{{ ques.title}}
{{ ques.id }}{{ ques.creat_time }}
{{ ques.author.username }}
{{ ques.detail }}
{% extends ‘index.html‘ %}
<meta charset="UTF-8">
<title>
    {% block title %}
        首頁
    {% endblock %}
</title> {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘css/shouye.css‘) }}"> {% endblock %} {% block body %} <div class="all" > {% for foo in username %} <div class="wai"> <li id="note-20391670"
data-note-id="20391670" class="have-img"> <div class="content"> <div class="author"> <a class="avatar" target="_blank" href="/u/deeea9e09cbc"> <img class="img" src="//upload.jianshu.io/users/upload_avatars/1442902/b54c023e8862.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/64/h/64" alt="64"> </a> <div class="info"> <a class="nickname" target="_blank" href="/u/deeea9e09cbc">{{ foo.author.username }}</a> <span class="time" data-shared-at="2017-11-30T08:15:03+08:00">{{ foo.creat_time }}</span> </div> </div> <a style="font-size: 20px" class="stitle" target="_blank" href="{{ url_for(‘detail‘,sent_id=foo.id) }}">{{ foo.title }}</a> <p class="abstract"> {{ foo.detail }}</p> <div class="meta"> <a class="collection-tag" target="_blank" href="/c/1hjajt">{{ biaoqian }}</a> </div> </div> </li> </div> {% endfor %} </div> {% endblock %}

建立評論的對象關系映射:

class Comment(db.Model):
__tablename__=‘comment

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‘))
    sent_id = db.Column(db.Integer, db.ForeignKey(‘sent.id‘))
    creat_time = db.Column(db.DateTime, default=datetime.now)
    detail = db.Column(db.TEXT, nullable=False)
    sent = db.relationship(‘Sent‘, backref=db.backref(‘comment‘))
    author = db.relationship(‘User‘, backref=db.backref(‘comment‘))

從首頁問答標題到問答詳情頁