简体   繁体   中英

How do I pass user input from html to python?

so I'm using bottle to connect my html and python code but I don't know how to take user input from my html code and use it as a variable in my python code. I have posted my code below, please help.

This is my bottle code

from bottle import default_app, route, template, post, request, get

@route('/')
def showForm():
    return template("form.html")

@post('/response')
def showResponse():
    return template("response.html")

application = default_app()

This is my main form which asks for the user input

<!DOCTYPE html>
<html lang = "en-us">
    <head>
        <title>BMI Calculator</title>
        <meta charset = "utf-8">
        <style type = "text/css">
            body{
                background-color: lightblue;
            }
        </style>
    </head>

<body>
    <h1>BMI Calculator</h1>
    <h2>Enter your information</h2>
    <form method = "post"
        action = "response">
        <fieldset>
            <label>Height: </label>
            <input type = "text"
            name = "feet">
            ft
            <input type = "text"
            name = "inches">
            in
            <br>
            <label>Weight:</label>
            <input type = "text"
            name = "weight">
            lbs
        </fieldset>

        <button type = "submit">
                Submit
        </button>
    </form>
</body>

And this is my response code which displays a page when the user hits submit, I embedded my python code to be able to calculate the user's bmi

<%
weight = request.forms.get("weight")
feet = request.forms.get("feet")
inches = request.forms.get("inches")
height = (feet * 12) + int(inches)
bmi = (weight/(height^2)) * 703
if bmi < 18.5:
    status = "underweight"

elif bmi < 24.9 and bmi > 18.51:
    status = "normal"

elif bmi > 25 and bmi < 29.9:
    status = "overweight"

else:
    status = "obese"
%>

<!DOCTYPE html>
<html lang = "en-us">
    <head>
        <title>Calculation</title>
        <meta charset = "utf-8">
    </head>
<body>
    <h1>Your Results</h1>
    <fieldset>
        <label>BMI : </label>
        <input type = "text"
        name = "BMI"
        value = {{bmi}}>
        <br>
        <label>Status:</label>
        <input type = "text"
        name = "status"
        value = {{status}}>
    </fieldset>
</body>

It looks like you don't even attempt to access your form data in POST route. forms property of bottle.request object holds all parsed form data. Bottle documentation provides a good example on how to handle form data.

Also, it's really a bad idea to put logic into templates beyond what is needed for correct page rendering. You need to process data in your route or to move processing into a separate module for better separation of responsibilities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM