Flask中的动态散列链接

时间:2013-03-25 13:45:00

标签: javascript python flask jinja2

好的,所以我在服务器端有这个代码:

@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 个答案:

答案 0 :(得分:1)

当你对它进行ajaxify时,你需要将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')