我正在學習燒瓶,並且遇到了一些問題。 我做了一個索引模板,其中有博客文章標題。用Flask創建博客
{% for title in titles %}
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="post-preview">
<a href="{{ url_for('post')}}">
<h2 class="post-title">
{{ title[0] }}
</h2>
</a>
<p class="post-meta">Posted by <a href="#">{{ author }}</a></p>
</div>
</div>
</div>
</div>
{% endfor %}
這是我的post.html模板的一部分。
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<p>{{ post_text1 | safe }}</p>
<hr>
<div class="alert alert-info" role="alert">Posted by
<a href="#" class="alert-link">{{ author }}</a>
</div>
</div>
</div>
</div>
我正在使用sqlite3。目前每個標題都會導致相同的post.html,其中第一個帖子是第一個文本。 如何使每個標題直接發佈到他們的發佈文本?我的意思是,如果我點擊第一個標題,它應該顯示post.html,並且應該有第一個文本。第二個標題應顯示第二個文本。 我應該編寫程序,爲每個帖子創建新的html還是有其他方法?
@app.route('/')
def index():
db = connect_db()
titles = db.execute('select title from entries')
titles = titles.fetchall()
author = db.execute('select author from entries order by id desc')
author = author.fetchone()
return render_template('index.html', titles=titles[:], author=author[0])
@app.route('/post/')
def post():
db = connect_db()
post_text1 = db.execute('select post_text from entries')
post_text1 = post_text1.fetchone()
author = db.execute('select author from entries where id=2')
author = author.fetchone()
return render_template('post.html', post_text1=post_text1[0], author=author[0])
你能發佈你的索引和發佈視圖嗎? –