html5调用ajax参数传递,使用python和flask,如何将动态路由参数传递给AJAX请求调用的新路由?...

我在Python Flask应用程序中创建了一个动态路由,以显示动态html模板。在页面的顶部有一个每个页面都不同的图形(wins_graph变量)。每个页面最初的显示方式都应该如此。在

在视图.py(初始路线)@app.route('/sports/nba/-spending-performance/')

def nba_spending_performance_team(team_abbr):

team_query = NBAWinsvsSalary.query.filter_by(team_abbr=team_abbr).order_by(NBAWinsvsSalary.season).all()

team_colors = 'RdBu'

wins_graph = Functions.wins_plot(team_query, 'No Seasons', 'rgba(175, 27, 50, 1)', team_colors)

return render_template('sports/nba/spending-performance-team.html', wins_graph)

体育/nba/消费表现-团队.html在

^{pr2}$

如您所见,图表上方有两个按钮。我希望显示第二个图形,但仅当单击第二个按钮时显示。我编写了一个AJAX调用来处理这个问题。在

在绘图.js在$('.atl_wins_graph').on('click',function(){

$('#selected').html(this.value);

$('.atl_wins_graph').removeClass('selected');

$(this).addClass('selected');

$.ajax({

url: "/spending-wins/",

type: "GET",

contentType: 'application/json;charset=UTF-8',

data: { 'selected': this.value },

dataType:"json",

success: function (data) {

Plotly.newPlot('wins_graph', data );

}

});

});

以及与新图形对应的附加路径。在

在视图.py(附加路线)@app.route('/spending-wins/', methods=['GET', 'POST'])

def spending_change_wins():

feature = request.args['selected']

team_color = 'rgba(175, 27, 50, 1)'

team_colors = 'RdBu'

team_query = NBAWinsvsSalary.query.filter_by(team_abbr=team_abbr).order_by(NBAWinsvsSalary.season).all()

graphJSON = wins_plot(team_query, feature, team_color, team_colors)

return graphJSON

问题在于附加路由中的team_query变量,因为我没有定义team_abbr

我收到的错误是:NameError: name 'team_abbr' is not defined

我明白我为什么会犯这个错误。如果我用数据库中的实际值来定义team_abbr,那么一切都会正常工作。但这并不是一个合理的解决方案,因为现在这条路线并不是动态的。在

我只是不知道如何在不使用重定向的情况下将team_abbr参数从初始路由传递到附加路由。任何帮助都将不胜感激。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值