English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
لقد رأينا أن يمكن تحديد http method في قواعد URL. يمكن لـ وظيفة URL mapping جمع بيانات النموذج التي تتلقاها كنموذج دليل، ويمكن توجيهها إلى النموذج لعرضها في الصفحة المطلوبة.
في هذا المثال، URL => / يعرض صفحة تحتوي على نموذج ( student.html)، والبيانات التي يتم ملئها يتم إرسالها إلى URL التي تطلق وظيفة result() => /result.
وظيفة results() تجمع بيانات النموذج الموجودة في request.form وتسلمها إلى result.html ويعرضها.
يقدم هذا النموذج ديناميكيا جدولا HTML يحتوي على بيانات النموذج.
فيما يلي معطيات برنامج تطبيق بايثون -
# Filename: example.py # Copyright: 2020 By w3codebox # Author by: ar.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: ar.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: ar.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>
تشغيل سكربت بايثون، ثم إدخال عنوان URL في المتصفح => http://localhost:5000/ . النتيجة كما يلي -
عند النقر على إرسالعند الضغط على الزر، تظهر بيانات النموذج في شكل جدول HTML result.html كما يلي -