我正在学习烧瓶,并且遇到了一些问题。 我做了一个索引模板,其中有博客文章标题。用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])
你能发布你的索引和发布视图吗? –