How to run script when HTML button is clicked (Python, Bottle)

İdris Bolge :

I want to run a script when the button with the bottle is pressed. But I get 404 errors every time. It says localhost: //File.py in the address bar, but I don't know how to route it.

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

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>
AKX :

You shouldn't use cgi and cgitb with Bottle, Flask, or any other Python web framework.

Try something like

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)

(and change the action of your form to action="/foo").

Also, consider using Flask; it's in the same vein as Bottle, but more popular and more maintained.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27862&siteId=1