Page 1 of 1
การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
Posted: 12/06/2020 1:07 pm
by bolue
การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
จะให้ไปทำฟังก์ชั่นไหน หรือ แสดงหน้าแรกของ html และหน้าอื่นๆ ของ html หรือส่งค่าไปหน้า html จะใช้แค่วิธี @app.route path แล้วให้ render_template เป็นการเรียกใช้ หรือจะส่งค่าไปด้วยพร้อมส่งค่า
และไม่รู้ว่า ถ้าหาก เอา class ครอบถับ @app.route จะยังสามารถเรียกใช้ /path ได้เลยเหมือนปกติมั้ย
เลยอยากทราบว่าวิธีการเขียน แบบ class object ของ python ทำยังไงหรอคะ
เพราะว่าปกติเคยใช้ แต่ @app.route ที่เป็น path ธรรมดาๆเลย

- C++ & java & Python-1.png (26.76 KiB) Viewed 582 times
ส่วนคลาส ก็เคยศึกษาและลองทำแบบนี้ ซึ้งผลลัพธ์มันจะแสดงผล อยู่แค่ข้างใน เป็นการสั่ง print

- C++ & java & Python-2.png (30.23 KiB) Viewed 582 times
Re: การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
Posted: 12/06/2020 4:46 pm
by jirawoot
ลองอั้นดูครับ ใช้ที่ต้องหรือเปล่า
Code: Select all
from flask import Flask, render_template
from flask_classful import FlaskView
app = Flask(__name__)
class TestView(FlaskView):
def __init__(self):
self.action = "TEST"
def index(self):
data = self.action
return render_template("home.html", data=data)
TestView.register(app,route_base = '/')
if __name__ == '__main__':
app.run(port=5050)
Re: การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
Posted: 12/06/2020 5:29 pm
by bolue
jirawoot wrote: ↑12/06/2020 4:46 pm
ลองอั้นดูครับ ใช้ที่ต้องหรือเปล่า
Code: Select all
from flask import Flask, render_template
from flask_classful import FlaskView
app = Flask(__name__)
class TestView(FlaskView):
def __init__(self):
self.action = "TEST"
def index(self):
data = self.action
return render_template("home.html", data=data)
TestView.register(app,route_base = '/')
if __name__ == '__main__':
app.run(port=5050)
ขอบคุณค่ะ
แล้วถ้าหนูส่งค่าจะ form ของ html ไป class python ยังไงคะพี่
Re: การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
Posted: 12/06/2020 6:23 pm
by jirawoot
สร้าง methon post มา ใน class แล้วใช้ request ของ flask ดึงค่าจาก html มา
python
Code: Select all
from flask import Flask, render_template, request
from flask_classful import FlaskView
app = Flask(__name__)
class TestView(FlaskView):
def __init__(self):
self.action = "TEST"
def index(self):
data = self.action
return render_template("home.html", data=data)
class TestView2(FlaskView):
def __init__(self):
self.action = "TEST"
def index(self):
data = self.action
return render_template("test.html", data=data)
def post(self):
req = request.form
print(req)
return "Method POST"
TestView.register(app,route_base = '/')
TestView2.register(app,route_base = '/test')
if __name__ == '__main__':
app.run(port=5050)
็html
Code: Select all
<form action="/test" method="POST">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
http://flask-classful.teracy.org/
Re: การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
Posted: 12/06/2020 6:27 pm
by jirawoot
Re: การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
Posted: 13/06/2020 10:34 am
by bolue