Flask中的动态哈希链接

2 投票
1 回答
801 浏览
提问于 2025-04-17 20:13

好的,我在服务器端有这段代码:

@app.route('/physical_graph') 
@login_required 
def physical_graph():
    # Some code
    return generate_graph_view()

如果我们点击那个圆形图标,就会得到这个链接/physical_graph#physical.circular:

<a class="icon" href="/physical_graph#physical.circular">
    <img src="{{url_for('static',filename='images/circular64x64.png')}}" 
         id="circular_icon" width="32" height="32" title="Circular layout"/>
</a>

我想问的是:我怎么能告诉Flask在这个#后面是什么字符串呢?我需要这个字符串,因为后面的代码是依赖于它的。我试着把app.route写成:

@app.route('/physical_graph#<some_string>')

但是没有成功——它给我返回了404错误。

有没有什么想法可以解决这个问题?

1 个回答

1

当你使用Ajax技术时,需要把physical.circle传给服务器。首先,这里提到的icon:只是一个使用上的说明。

<!-- notice i change from icon -> graphIcon icon is too general  -->
<!--a stylist would be angry at you for using icon -->
<a class="graphIcon" href="/physical_graph#physical.circular"> .. </a>

你有几个选择,首先可以把它放在数据属性里,这个数据属性会放在JavaScript代码中(这段代码只会执行一次)。

jQuery("#circular_icon").click(function() {
   $.ajax({
         url:'/physical_graph', 
         data:{
               'graph_type': 'physical_graph.circular'
         },
         'success':function(data){
             //your function goes here. 
         }
   })
} );

@app.route('/physical_graph') 
@login_required 
def physical_graph():
  _type = request.args.get('graph_type')

撰写回答