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

Don*_*the 5 python jinja2 flask

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

<a href="http://www.example.com/stuff.html#exactlocation">Go there</a> 
Run Code Online (Sandbox Code Playgroud)

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

@main.route('/exactlocation')
def exactlocation():
    return render_template('stuff.html#exactlocation')
Run Code Online (Sandbox Code Playgroud)

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

Don*_*the 9

感谢dirn的评论,我设法使用下面的代码.

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

导航菜单:

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

烧瓶路线:

@main.route('/')
def stuff():
    return render_template('stuff.html')
Run Code Online (Sandbox Code Playgroud)

stuff.html:

...
<section id="exactlocation">
...
Run Code Online (Sandbox Code Playgroud)

  • 这也可以动态工作,如:`&lt;a href=" {{ url_for('comments', _anchor=comment.id) }}"&gt;` (2认同)