2017-11-18 1 views
1

병을 사용하여 HTML과 파이썬 코드를 연결했지만 HTML 코드에서 사용자 입력을 받아 내 파이썬 코드에서 변수로 사용하는 방법을 모르겠습니다. 아래에 제 코드를 게시했습니다. 제발 도와주세요. html에서 사용자 입력을 어떻게 파이썬으로 전달합니까?

이 사용자 입력

<!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> 

요청 내 주요 양식 내 병 코드

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

되며,이 때 페이지를 표시 내 응답 코드 사용자 조회 제출, 사용자의 bmi를 계산할 수 있도록 내 Python 코드를 임베드했습니다.

<% 
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> 

답변

0

POST 경로에서 양식 데이터에 액세스하려고 시도하지 않은 것으로 보입니다. forms 속성은 bottle.request 개체의 모든 구문 분석 된 양식 데이터를 보유합니다. Bottle documentation은 양식 데이터를 처리하는 방법에 대한 좋은 예를 제공합니다.

올바른 페이지 렌더링을 위해 필요한 것을 넘어서 로직을 템플릿에 넣는 것은 정말 나쁜 생각입니다. 책임을보다 효과적으로 분리하려면 경로에서 데이터를 처리하거나 처리를 별도의 모듈로 옮겨야합니다.

관련 문제