Python Flask 使用按钮调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30899484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Flask calling functions using buttons
提问by Sean
Once the button in my flask template is pressed I'd like it to call a python function defined in app.py that I made to be available to be called within the template by typing the following below where I define the function:
按下我的烧瓶模板中的按钮后,我希望它调用 app.py 中定义的 python 函数,我可以通过在下面定义函数的位置键入以下内容在模板中调用该函数:
Example function in app.py:
app.py 中的示例函数:
@app.route('/foo')
def foo(x,y):
pass
app.jinja_env.globals.update(foo=foo)
Template:
模板:
<button type="button" onclick="myFunction(this)" name="enable" id="{{counter}}"> Enable </button>
In my button I have the onclick attribute just to test that the correct button out of many is pressed using javascript like such:
在我的按钮中,我有 onclick 属性只是为了测试使用 javascript 按下了正确的按钮,如下所示:
{% block scripts %}
{{ super() }}
<script>
function myFunction(elem){
if(confirm('Are you sure you want to ' + elem.name) == true){
alert("its done.");
}
else {
return false;
}
}
</script>
{% endblock %}
The issue I'm facing is I need the function that I'm making available within the template to correspond to the correct button. For example, if the button says Enable, then I need to call the enable function defined already or otherwise if the button corresponds to false, I'd like the disable function to be used.
我面临的问题是我需要在模板中提供的功能与正确的按钮相对应。例如,如果按钮显示启用,那么我需要调用已经定义的启用功能,否则如果按钮对应于假,我希望使用禁用功能。
I feel like I'm headed in the right direction but can't get past this part. Please be as detailed as you can.
我觉得我正朝着正确的方向前进,但无法越过这部分。请尽可能详细。
采纳答案by junnytony
If you want to execute your function without generating a request to the server, then your function must be defined in JavaScript. Otherwise, you need to fire an HTTP request.
如果您想在不向服务器生成请求的情况下执行您的函数,那么您的函数必须在 JavaScript 中定义。否则,您需要触发 HTTP 请求。
Now in your case, if all you're trying to do is enable/disable buttons, it would make sense to do all that in javascript (no need to go to the server).
现在,在您的情况下,如果您要做的只是启用/禁用按钮,那么在 javascript 中执行所有操作是有意义的(无需转到服务器)。
Example:
例子:
<button type="button" onclick="disableButton(this)" name="enable">Enable</button>
javascript
javascript
function disableButtonState(elem) {
if(confirm('Are you sure you want to disable this button?') == true) {
elem.disabled = true;
alert("its done.");
}
else {
return false;
}
}
</script>
However if what you want is to call a method on your server that, for example, sends an email, then you should use a form
POST/GET or AJAX
POST/GET
但是,如果您想要调用服务器上的方法,例如发送电子邮件,那么您应该使用form
POST/GET 或AJAX
POST/GET
Example:
例子:
app.py
应用程序
@app.route('/foo', methods=['GET', 'POST'])
def foo(x=None, y=None):
# do something to send email
pass
template
模板
<form action="/foo" method="post">
<button type="submit" value="Send Email" />
</form>
When you click the "Send Email" button an HTTP POST request is sent to "/foo" on your application. Your function foo
can now extract some data from the request and do whatever it wants to do on the server side and then return a response to the client web browser.
当您单击“发送电子邮件”按钮时,一个 HTTP POST 请求将发送到您应用程序上的“/foo”。您的函数foo
现在可以从请求中提取一些数据并在服务器端执行它想做的任何事情,然后将响应返回给客户端 Web 浏览器。
It would suggest going through the Flask Tutorialto get a better understanding of client/server interactions when it comes to web applications built with Flask.
当涉及到使用 Flask 构建的 Web 应用程序时,建议阅读Flask 教程以更好地理解客户端/服务器交互。