Как запустить скрипт при нажатии кнопки HTML (Python, Bottle) - PullRequest
2 голосов
/ 05 марта 2020

Я хочу запустить скрипт, когда нажата кнопка с bottle. Но я получаю 404 ошибки каждый раз. В адресной строке написано localhost: //File.py, но я не знаю, как его маршрутизировать.

app.py

from bottle import *

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


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

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

денем. 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>

1 Ответ

2 голосов
/ 05 марта 2020

Вы не должны использовать cgi и cgitb с Bottle, Flask или любым другим Python веб-фреймворком.

Попробуйте что-то вроде

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, но более популярен и поддерживается.

...