简体   繁体   English

如何将参数从前端传递到python脚本

[英]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.

相关问题 如何将数据从 React 前端 Hooks 传递到 Express 后端 - How to pass data from React front end Hooks to Express backend 如何将对象从前端传递给Struts2? - How to pass an object from front-end to Struts2? 如何将对象从哈巴狗传递到前端javascript - How to pass an object from pug to front end javascript Google App Script WebApp Array 不会从后端传递到前端 - Google App Script WebApp Array doesn't pass from backend to front end 如何在node.js ejs模板中将变量从后端传递到前端 - How to pass variable from back-end to front-end in node.js ejs template 如何将字符串从后端(java)传递到前端(打字稿)并让它出现在 UI 上? - How to pass a string from back end (java) to the front end(typescript) and have it appear on UI? 在Node / Express中将用户输入从前端传递到后端 - Pass user input from front end to back end in Node/Express ajax将参数传递给python脚本 - ajax pass parameters to python script 如何使用列表中的 Python 将我在前端提交的数据传递到后端? - How do I pass the data that I have submitted in front end to backend using Python in a list? 如何通过JSON将表数据从HTML前端和HTML前端传递到Django中的服务器端? - How to pass a table data from and HTML front end to the server side in Django through JSON?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM