Link 到 Flask 模板中的特定位置
Link to a specific location in a Flask template
在 HTML 中,我可以 link 直接到页面上的特定位置,假设有一个具有给定 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')
如何 link 到模板中的特定位置?
感谢 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">
...
在 HTML 中,我可以 link 直接到页面上的特定位置,假设有一个具有给定 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')
如何 link 到模板中的特定位置?
感谢 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">
...