链接到Flask模板中的特定位置

发布于 2021-01-29 18:04:36

在HTML中,假设有一个具有给定id的元素,我可以直接链接到页面上的特定位置:

<a href="http://www.example.com/stuff.html#exactlocation">Go there</a>

在Flask中,我尝试将锚添加到,render_template但得到了jinja2.exceptions.TemplateNotFound: stuff.html#exactlocation

@main.route('/exactlocation')
def exactlocation():
    return render_template('stuff.html#exactlocation')

如何链接到模板中的特定位置?

关注者
0
被浏览
146
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    多亏了dirn的评论,我设法将其与下面的代码一起使用。

    传递_anchor关键字以url_for将锚添加到生成的URL。

    导航菜单:

    <a href="{{ url_for('.stuff', _anchor='exactlocation') }}">Go to specific id on suff page</a>
    

    烧瓶路线:

    @main.route('/')
    def stuff():
        return render_template('stuff.html')
    

    stuff.html

    ...
    <section id="exactlocation">
    ...
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看