【问题标题】:Django: How to call a function once a button is clicked?Django:单击按钮后如何调用函数?
【发布时间】:2016-12-08 11:04:03
【问题描述】:

我的views.py 中编写了一个发送子进程命令的方法。

我希望能够单击我的 Django 应用程序上的一个按钮,它会启动我编写的方法。

我该怎么做?

这是我在views.py中的当前函数

    def send_command(request):
        output = subprocess.check_output('ls', shell=True)
        print(output)
        return render(request, 'button.html')

我在 .html 文件中也有一个按钮

    <center><button type='submit' class='btn btn-lg'>Button</button></center>

我对此非常陌生,因此我们将不胜感激。请随时询问更多信息。

【问题讨论】:

  • @Taylor 我相信你链接的问题与 Tkinter 而不是 Django/html 相关
  • this 可能会有所帮助
  • @an0o0nym 太好了,我去看看!谢谢

标签: python html django methods views


【解决方案1】:

如果您不发送将保存在服务器中的内容,您可以将按钮更改为 url 的行为

url.py

from views import send_command

urlpatterns = [
    url(r'^send_command$', send_command, name='send_command'),
]

html

<a href="{% url 'send_command' %}" class='btn btn-lg'>Button</a>

【讨论】:

  • 谢谢,我试试。但是,有没有什么方法可以在不切换页面的情况下调用方法呢?
  • 是的,像@an0o0nym 的评论一样使用ajax
  • 太好了,我两个都试试!我很感激!
猜你喜欢
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 2020-08-29
  • 1970-01-01
  • 2020-12-23
  • 1970-01-01
  • 1970-01-01
  • 2022-11-14
相关资源
最近更新 更多