0
我试图将用户在前端定义的参数传递给正在服务器上运行的python脚本。我在后端使用了一个Django框架(目前可能对我的目的来说过于沉重,但想试一试)。这里是我用来调用位于定义的url的python fxn的jQuery代码。我想要传递的参数在data属性中。如何将参数从前端传递到python脚本
bbviz.onStart = (function() {
$.ajax({
type:'get',
url:'http://localhost:8000/statview/simulation/',
data:{url:'blackch02', 'profile':'current', 'sims':1000},
async:'asynchronous',
dataType:'json',
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error);
}
});
});
而这里的Django的视图功能:
def simulation(request):
out = batterSim.sim_season(url, profile, sims)
return out
我打电话batterSim模块和运行具有相同的命名参数作为数据属性sim_season功能。该函数然后返回前端将收到的json对象。所以我想弄清楚如何从ajax函数中获取这些参数并在python函数中使用它们。
这是完美的感谢!我知道必须有这样简单的事情。 –
我建议使用request.GET.get('profile'),如果该属性不存在,这将返回None,如果您尝试使用['profile']访问该属性并且它不存在,将会引发例外。 –