Python Flask使用按钮调用函数 [英] Python Flask calling functions using buttons

查看:691
本文介绍了Python Flask使用按钮调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python / Flask很陌生,特别是脚本语言,所以请和我一起裸照。

一旦按下烧瓶模板中的按钮,我想调用一个在app.py中定义的python函数,我可以在通过在下面定义函数的地方输入以下内容:

app.py中的示例函数:

<$ p $ ($,$)

app.jinja_env.globals.update(foo = foo )

模板:

 < button type =buttononclick =myFunction(this)name =enableid ={{counter}}>启用< / button> 

在我的按钮中,我有onclick属性只是为了测试正在使用的按钮这样的javascript:

pre $ {$ block $%$ b $ {{super()}}
< ;脚本>
函数myFunction(elem){
if(confirm('你确定要'+ elem.name)== true){
alert(its done。);
}
else {
return false;
}
}
< / script>
{%endblock%}

我要面对的问题是我正在模板中提供对应于正确的按钮。例如,如果按钮说Enable,那么我需要调用已经定义的启用函数,否则,如果按钮对应于false,我想禁用函数被使用。



我觉得我正朝着正确的方向前进,但是无法超越这个部分。请尽可能详细。

解决方案

如果你想执行你的函数而不生成对服务器的请求,那么你的函数必须在JavaScript中定义。否则,你需要发起一个HTTP请求。

现在在你的情况下,如果你所要做的只是启用/禁用按钮,那么在JavaScript中执行所有操作是有意义的(不需要去



$ b $ < buttononclick =disableButton(this)name =enable>启用< / button>

javascript

  function disableButtonState(elem){
if(confirm('你确定要关闭这个按钮?'== true){
elem.disabled = true;
alert(完成了);
}
else {
return false;
}
}
< / script>然而,如果你想要的是调用你的服务器上的一个方法,例如,发送一个电子邮件,那么你应该使用 form POST / GET或 AJAX POST / GET

示例:

app.py

  @app.route('/ foo',methods = ['GET','POST'])
def foo(x = None,y = None):
#做一些事情发送电子邮件
通过

模板

 < form action =/ foomethod =post> 
< button type =submitvalue =发送电子邮件/>
< / form>当您单击发送电子邮件按钮时,HTTP POST请求将被发送到/ foo上你的申请。您的函数 foo 现在可以从请求中提取一些数据,并在服务器端执行任何想要的操作,然后向客户端Web浏览器返回响应。



建议通过 Flask教程,以便更好地了解与Flask构建的Web应用程序的客户端/服务器交互。


I'm pretty new to Python/Flask and especially scripting languages so please bare with me.

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:

Example function in 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:

{% 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.

解决方案

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.

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).

Example:

<button type="button" onclick="disableButton(this)" name="enable">Enable</button>

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

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.

It would suggest going through the Flask Tutorial to get a better understanding of client/server interactions when it comes to web applications built with Flask.

这篇关于Python Flask使用按钮调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆