【问题标题】:Unable to redirect individual blog posts to new page with their related content - Flask Blog无法将单个博客文章及其相关内容重定向到新页面 - Flask Blog
【发布时间】:2021-09-14 07:04:24
【问题描述】:

我正在构建一个烧瓶博客应用程序,目前在每个博客文章下都有一个更多信息按钮,该按钮应该重定向到博客文章内容所在的位置。但是,我正在努力重定向 - 页面一直显示为空白。

这是我的 routes.py 文件的一部分

@app.route("/moreinfo/<id>", methods=['GET', 'POST'])
def moreinfo(id):
    current_post = Job_Requirements.query.filter_by(id=id).first_or_404()
    return render_template('moreinfo.html', title='More Info', current_post=current_post)

这是我的 jobs.html 文件,上面有所有的工作/博客文章。下面是末尾的更多信息按钮,它应该将您重定向到每个帖子的内容页面 - 由他们的 id 指定。

<!DOCTYPE html> {% extends "layout.html" %} {% block content %} {% for post in posts %}
<article class="media content-section">
    <div class="media-body">
        <div class="article-metadata">
            <a class="mr-2" href="#">{{ post.company }}</a>
            <small class="text-muted">{{ post.date_posted }}</small>
        </div>
        <h2><a class="article-title" href="#">{{ post.job_title }}</a></h2>
        <p class="article-content">{{ post.sector }}</p>
        <p class="article-content">{{ post.location }}</p>
        <p class="article-content">{{ post.employment_type }}</p>
        <a href="{{ url_for('moreinfo', id=post.id)}}" class="btn btn-primary text-right">More Info</a>
    </div>
</article>
{% endfor %} {%endblock content %}

这是我的 Moreinfo.html 文件,每个博客帖子内容都应该打开。

<!DOCTYPE html> {% extends "layout.html" %} {% block content %} {% for post in posts %}
<h1>More Info</h1>
<article class="media content-section">
    <p class="article-content">{{ post.description }}</p>
    <ul class="list-unstyled">
        <li>This is a list.</li>
        <li>It appears completely unstyled.</li>
        <li>Structurally, it's still a list.</li>
        <li>However, this style only applies to immediate child elements.</li>
        <li>Requirements:
            <ul>
                <li class="article-content"></li>{{ post.requirement_1 }}</li>
        <li class="article-content"></li>{{ post.requirement_2 }}</li>
        <li class="article-content"></li>{{ post.requirement_3 }}</li>
        <li class="article-content"></li>{{ post.requirement_4 }}</li>
        <li class="article-content"></li>{{ post.requirement_5 }}</li>
        <li class="article-content"></li>{{ post.requirement_6 }}</li>
        <li>will still show a bullet</li>
        <li>and have appropriate left margin</li>
        </ul>
        </li>
        <li>This may still come in handy in some situations.</li>
    </ul>
</article>
{% endfor %} {%endblock content %}

任何帮助都将不胜感激,我在如何解决这个问题上困扰了这么久。

【问题讨论】:

    标签: python html mysql flask jinja2


    【解决方案1】:

    在您的 Moreinfo.html 文件中,您仍然循环访问变量 posts,但在路由中您直接为模板提供 current_post。由于您目前没有遍历任何内容,因此浏览器中没有显示任何内容。

    【讨论】:

    • 啊,好吧,那么在我的 moreinfo.html 文件中 - 我会做 {% for post in current_post %} 然后在我的路由文件中 render_template('moreinfo.html', title='更多信息', current_post=current_post)
    • 其实我刚才写的都试过了,还是不行
    • 你省略了循环,而不是post,使用current_post,例如current_post.description
    猜你喜欢
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    相关资源
    最近更新 更多