【问题标题】:How to run script when HTML button is clicked (Python, Bottle)单击 HTML 按钮时如何运行脚本(Python、Bottle)
【发布时间】:2020-03-05 19:34:22
【问题描述】:

我想在按下带有瓶子的按钮时运行一个脚本。但我每次都会收到 404 错误。它在地址栏中显示 localhost: //File.py,但我不知道如何路由它。

app.py

from bottle import *

@route('/')
def home():
    return template('deneme.html')


run(host='localhost',port=8080)

文件.py

#!/usr/bin/python
import cgi, cgitb
form =  cgi.FieldStorage


username = form["username"].value
emailaddress = form["emailaddress"].value



print("Content-type: text/html\r\n\r\n")
print( "<html>")
print("<head>")
print("<title>First Script</tittle>")
print("</head")
print("<body>")
print("<h3>This is HTML's Body Section</h3>")
print(username)
print(emailaddress)
print("</body>")
print("</html>")

deneme.html

<html>
  <head>
  <meta charset="UTF-8">
    <title>Document</title>

  </head>
  <body>
  <form action="File.py" method="post">
    username: <input type="text" name="username"/>
    <br />
    Email Adress: <input type="email" name="emailaddress"/>
<input type="submit" name="Submit">
    </form>
  </body>
</html>

【问题讨论】:

  • 您能否为您的声明提供更多细节或图片It says localhost: //File.py in the address bar, but I don't know how to route it.?我认为如果他们知道it 是什么,并且可以看到单击按钮时发生的情况,这将有助于其他人回答您的问题。
  • 问题变了。下面的新问题

标签: python bottle


【解决方案1】:

您不应将 cgicgitb 与 Bottle、Flask 或任何其他 Python Web 框架一起使用。

试试类似的东西

from bottle import run, route, request

@route('/')
def home():
    return template('deneme.html')

@route('/foo')
def foo():
    return '%s %s' % (request.forms.username, request.forms.email)

run(host='localhost',port=8080)

(并将表单的操作更改为action="/foo")。

另外,考虑使用 Flask;它与 Bottle 风格相同,但更受欢迎且维护得更好。

【讨论】:

  • 您不应该将 cgi 和 cgitb 与 Bottle、Flask 或任何其他 Python Web 框架一起使用。为什么?
  • 因为它们不会互操作。 cgi 仅用于原始 CGI 程序,如果您在 Web 框架下运行,它将处理 CGI 输入/输出。
  • 那么我该怎么做呢?你能给我一个想法吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-09
  • 2023-02-22
  • 2017-04-08
  • 1970-01-01
  • 2017-02-27
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多