烧瓶页面未显示应显示的内容

时间:2020-08-07 17:32:47

标签: python flask

帖子页面也必须显示传递给它的帖子,但它仅显示继承给它的布局。

routes.py

@app.route("/post/<int:post_id>")
def post(post_id):
    post = Post.query.get_or_404(post_id)
    return render_template('post.html', title=post.title, post=post)

post.html

{% block content %}
    {% for post in posts %}
    <article class="media content-section">
            <img class="rounded-circle article-img" src="{{ url_for('static', filename='pictures/' + post.author.image_file) }}">
          <div class="media-body">
            <div class="article-metadata">

              <a class="mr-2" href="#">{{ post.author.username }}</a>
              <small class="text-muted">{{ post.date_posted.strftime('%Y-%m-%d') }}</small>
            </div>
            <h2 class="article-title">{{ post.title }}</h2>
            <p class="article-content">{{ post.content }}</p>
          </div>
        </article>
    {% endfor %}
{% endblock content %}```


2 个答案:

答案 0 :(得分:1)

您在html页面中不需要for循环,请从以下位置替换您的post.html

{% block content %}
{% for post in posts %}
<article class="media content-section">
        <img class="rounded-circle article-img" src="{{ url_for('static', filename='pictures/' + post.author.image_file) }}">
      <div class="media-body">
        <div class="article-metadata">

          <a class="mr-2" href="#">{{ post.author.username }}</a>
          <small class="text-muted">{{ post.date_posted.strftime('%Y-%m-%d') }}</small>
        </div>
        <h2 class="article-title">{{ post.title }}</h2>
        <p class="article-content">{{ post.content }}</p>
      </div>
    </article>
{% endfor %}
{% endblock content %}

收件人:

{% block content %}
    <article class="media content-section">
            <img class="rounded-circle article-img" src="{{ url_for('static', filename='pictures/' + post.author.image_file) }}">
          <div class="media-body">
            <div class="article-metadata">

              <a class="mr-2" href="#">{{ post.author.username }}</a>
              <small class="text-muted">{{ post.date_posted.strftime('%Y-%m-%d') }}</small>
            </div>
            <h2 class="article-title">{{ post.title }}</h2>
            <p class="article-content">{{ post.content }}</p>
          </div>
        </article>
   {% endblock content %}```


答案 1 :(得分:1)

模板使用时

{% for post in posts %}

期望posts是一个包含各个帖子的Iterable(例如列表)。

这条路线将模板传递给post中的单个帖子。由于未传递模板posts,因此会将其视为空列表。

删除for循环,但不删除其内容。