2016-03-07 59 views
1

在HTML我可以直接链接到一个特定位置的页面上,假设有给定id的元素:我试图添加锚链接到一个特定的位置,在烧瓶模板

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

在瓶到render_template但我得到jinja2.exceptions.TemplateNotFound: stuff.html#exactlocation

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

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

+1

这是浏览器的行为。只要正常渲染你的模板,浏览器就会处理它(假设你在页面上有指定的锚点)。 – dirn

+0

@dirn我不确定你的意思。如果我使用return render_template('stuff.html'),浏览器如何知道去页面上的特定位置而不是顶部,请在页面加载时运行javascript检查? –

+1

浏览器将自动跳到已命名的锚点,不需要JavaScript。唯一的要求是HTML中存在已命名的锚(即'')。 – dirn

回答

2

感谢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"> 
...