链接到 Flask 模板中的特定位置 [英] Link to a specific location in a Flask template

查看:17
本文介绍了链接到 Flask 模板中的特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In HTML I can link directly to a specific location on a page, assuming there's an element with the given id:

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

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

In Flask I tried to add the anchor to render_template but I get jinja2.exceptions.TemplateNotFound: stuff.html#exactlocation.

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

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

How do I link to a specific location in a template?

推荐答案

多亏了 dirn 的评论,我才设法使用下面的代码来解决这个问题.

Thanks to dirn's comments I managed to get this working with the code below.

_anchor 关键字传递给 url_for 以将锚点附加到生成的 URL.

Pass the _anchor keyword to url_for to append an anchor to the generated 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">
...

这篇关于链接到 Flask 模板中的特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆