问题描述
多亏了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中,假设有一个具有给定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')
如何链接到模板中的特定位置?