【问题标题】:How do I link a button in html with my python code? (django)如何将 html 中的按钮与我的 python 代码链接? (django)
【发布时间】:2020-07-06 21:37:13
【问题描述】:

我正在使用 django,我想根据用户在我页面的选择框中输入的内容来计算一些东西,但我不知道如何将它与计算所有内容的 python 代码连接起来,请帮助:(

【问题讨论】:

  • 您好,欢迎来到 StackOverflow!请分享您拥有的代码以及您已经尝试过的代码,以便我们帮助您找出问题所在。

标签: python html django


【解决方案1】:

您可以将这样的 html 表单元素与方法 post 和 action="path_of_your_python_file" 一起使用

<form name="search" action="calculate.py" method="GET">
      Search: <input type="checkbox" name="user_input">
      <input type="submit" value="Submit">
</form>

然后在calculate.py中

import cgi
form = cgi.FieldStorage()
searchterm =  form.getvalue('user_input')
// Do your calculation here

我认为这会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-07
    • 2011-08-06
    • 1970-01-01
    • 2015-01-30
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    相关资源
    最近更新 更多