javascript - Flask 中的动态散列链接

标签 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。

有什么办法吗?

最佳答案

当你对它进行 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')

关于javascript - Flask 中的动态散列链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15616504/

相关文章:

python - 此 python 代码中此 POST 方法的 json 请求是否正确?

javascript - 停止声音并分离阵列

javascript - AngularJS:从 Controller 函数绑定(bind)属性值

javascript - 如何在使用 javascript 验证一键后禁用按钮?

python - PyQt4:将列表保存到 QSettings

python - curl with POST 不工作,request.POST 为空

python - Pandas 数据帧 : Remove row satisfying certain condition

javascript - 如何使用 this.props.children 将 Prop 从父级传递给子级?

python - 从另一个文件导入方法的问题

javascript - 在Python中将二进制字符串解释为图像,由Javascript native 方法生成