链接到Flask模板中的特定位置 [英] Link to a specific location in a Flask template
本文介绍了链接到Flask模板中的特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在HTML中,我可以直接链接到页面上的特定位置,假设有一个给定ID的元素:
< a href =http://www.example.com/stuff.html#exactlocation>去那里< / a>
在Flask中我尝试添加锚点到 render_template
但我得到 jinja2.exceptions.TemplateNotFound:stuff.html#exactlocation
。
@ main.route('/ exactlocation')
def exactlocation():
return render_template('stuff.html#exactlocation')
如何链接到模板中的特定位置?
解决方案
通过
_anchor
关键字 url_for
将一个锚点添加到生成的URL。 导航菜单:
< a href ={{url_for('。stuff',_anchor ='exactlocation')}}>转至特定页面< / a>
烧瓶路线:
@ main.route('/')
def stuff():
return render_template('stuff.html')
stuff.html
:
...
< section id =exactlocation>
...
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>
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?
解决方案
Thanks to dirn's comments I managed to get this working with the code below.
Pass the _anchor
keyword to url_for
to append an anchor to the generated URL.
nav menu:
<a href="{{ url_for('.stuff', _anchor='exactlocation') }}">Go to specific id on suff page</a>
Flask route:
@main.route('/')
def stuff():
return render_template('stuff.html')
stuff.html
:
...
<section id="exactlocation">
...
这篇关于链接到Flask模板中的特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文