English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
URL 규칙에서 http 메서드를 지정할 수 있습니다. URL 매핑 함수는 양식 데이터를 딕셔너리 객체로 수집하여 해당 웹 페이지에 표시하기 위해 템플릿에 전달합니다
다음 예제에서 URL => / 양식을 포함한 웹 페이지를 표시합니다( student.html) 데이터는 result() 함수를 트리거하는 URL =>에 제출됩니다 /result에 있습니다
results() 함수는 request.form에 존재하는 양식 데이터를 수집하여 result에 전달합니다 result.html 및 표시합니다
이 템플릿은 양식 데이터를 HTML 테이블로 동적으로 표시합니다
아래는 Python 애플리케이션 코드입니다 -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : ko.oldtoolbag.com # Date : 2020-08-08 from flask import Flask, render_template, request app = Flask(__name__) @app.route('/) def student(): return render_template('student.html') @app.route('/result', methods=['POST', 'GET']) def result(): if request.method == 'POST': result = request.form return render_template("result.html", result=result) if __name__ == '__main__': app.run(debug=True)
아래는 student.html 의 HTML 스크립트 코드입니다。
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : ko.oldtoolbag.com # Date : 2020-08-08 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Flask 예제</title> </head> <body> <form action="http://localhost:5000/result" method="POST"> <p>이름 <input type="text" name="Name"> /></p> <p>물리 점수: <input type="text" name="Physics"> /></p> <p>화학 점수: <input type="text" name="Chemistry"> /></p> <p>수학 점수: <input type="text" name="Mathematics"} /></p> <p><input type = "submit" value = "제출" /></p> </form> </body> </html>
템플릿 코드(result.html)은 다음과 같습니다 -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : ko.oldtoolbag.com # Date : 2020-08-08 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Flask 예제</title> </head> <body> <table border = 1> {% for key, value in result.items() %} <tr> <th> {{ key }} </th> <td> {{ value }} </td> </tr> {% endfor %} </table> </body> </html>
Python 스크립트를 실행하고 브라우저에서 URL을 입력하면 => http://localhost:5000/ 결과는 다음과 같습니다 -
클릭할 때 제출버튼을 클릭할 때, 양식 데이터는 HTML 테이블 형식으로 표시됩니다 result.html 아래와 같이 중국어로 -