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

2024-01-24

在 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')

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


感谢 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">
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

链接到 Flask 模板中的特定位置 的相关文章

随机推荐