【发布时间】:2016-09-27 14:22:01
【问题描述】:
我正在学习烧瓶,我有一个小问题。 我做了一个索引模板,博客文章标题在哪里。
{% 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])
【问题讨论】:
-
您能发布您的索引和视图吗?