[英]How to pass parameters from front end to python script
I'm trying to pass the parameters defined by a user in the front end to a python script being run on the server. 我正在尝试将前端用户定义的参数传递给在服务器上运行的python脚本。 I'm using a Django framework in the back end (probably too heavy for my purposes at the moment but wanted to give it a try). 我在后端使用Django框架(目前可能对我来说太重了,但想尝试一下)。 Here's the jQuery code I'm using to call the python fxn which is located at the defined url. 这是我用来调用位于定义的URL处的python fxn的jQuery代码。 The parameters I'd like to pass are in the data attribute. 我想传递的参数在data属性中。
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);
}
});
});
And here's the Django view function: 这是Django视图功能:
def simulation(request):
out = batterSim.sim_season(url, profile, sims)
return out
I'm calling the batterSim module and running the sim_season function which has the same named parameters as the data attribute. 我正在调用batterSim模块并运行sim_season函数,该函数具有与data属性相同的命名参数。 The function then returns a json object that the front end would receive. 然后,该函数返回前端将接收的json对象。 So I'm trying to figure out how to take those parameters from the ajax function and use them in the python function. 因此,我试图找出如何从ajax函数中获取这些参数,并将其用于python函数中。
You can use parameter in Django view by call 您可以通过调用在Django视图中使用参数
request.GET['profile']
request.GET['sims']
You should read more about Django request : https://docs.djangoproject.com/en/1.10/ref/request-response/#django.http.HttpRequest.GET 您应该阅读有关Django请求的更多信息: https : //docs.djangoproject.com/en/1.10/ref/request-response/#django.http.HttpRequest.GET
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.