Skip to main content
 首页 » 编程设计

flask 渲染模板不与 anchor 标记一起工作#之jinja2.exceptions.TemplateNotFound

2025年05月04日129wayfarer

在 url_for 方法中,您可以传入 _anchor 参数以在页面加载时加载 anchor 标记。

我将如何使用 render_template 做同样的事情?

我可以

render_template('profile/index.html')  

没有任何问题,但是当我在 html 末尾添加 anchor 标记并尝试执行时
render_template('profile/index.html#h1") 

我收到找不到模板的错误
jinja2.exceptions.TemplateNotFound 
TemplateNotFound: profile/index.html#h1 

请您参考如下方法:

基本上这就是我最终要做的,我传入了一个值以及 render_template

return render_template('profile/index.html', set_tab=1) 

在 javascript 客户端,我执行以下操作来设置选项卡
<script> 
   var obj_load={% if set_tab==1%}1{%else%}0{%endif%}; 
   tab_load=(obj_load?"#h1":"#"); 
 
   $(document).ready(function () { 
       if(location.hash || obj_load) { 
       $('a[href=' + (location.hash ||  tab_load) + ']').tab('show'); 
       } 
    }); 
 
</script> 

仅供引用,我正在使用 bootstrap 3 的选项卡 Pane