การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
Moderators: mindphp, ผู้ดูแลกระดาน
-
- PHP VIP Members
- Posts: 3029
- Joined: 04/06/2020 10:05 am
การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
จะให้ไปทำฟังก์ชั่นไหน หรือ แสดงหน้าแรกของ html และหน้าอื่นๆ ของ html หรือส่งค่าไปหน้า html จะใช้แค่วิธี @app.route path แล้วให้ render_template เป็นการเรียกใช้ หรือจะส่งค่าไปด้วยพร้อมส่งค่า
และไม่รู้ว่า ถ้าหาก เอา class ครอบถับ @app.route จะยังสามารถเรียกใช้ /path ได้เลยเหมือนปกติมั้ย
เลยอยากทราบว่าวิธีการเขียน แบบ class object ของ python ทำยังไงหรอคะ
เพราะว่าปกติเคยใช้ แต่ @app.route ที่เป็น path ธรรมดาๆเลย ส่วนคลาส ก็เคยศึกษาและลองทำแบบนี้ ซึ้งผลลัพธ์มันจะแสดงผล อยู่แค่ข้างใน เป็นการสั่ง print
จะให้ไปทำฟังก์ชั่นไหน หรือ แสดงหน้าแรกของ html และหน้าอื่นๆ ของ html หรือส่งค่าไปหน้า html จะใช้แค่วิธี @app.route path แล้วให้ render_template เป็นการเรียกใช้ หรือจะส่งค่าไปด้วยพร้อมส่งค่า
และไม่รู้ว่า ถ้าหาก เอา class ครอบถับ @app.route จะยังสามารถเรียกใช้ /path ได้เลยเหมือนปกติมั้ย
เลยอยากทราบว่าวิธีการเขียน แบบ class object ของ python ทำยังไงหรอคะ
เพราะว่าปกติเคยใช้ แต่ @app.route ที่เป็น path ธรรมดาๆเลย ส่วนคลาส ก็เคยศึกษาและลองทำแบบนี้ ซึ้งผลลัพธ์มันจะแสดงผล อยู่แค่ข้างใน เป็นการสั่ง print
- jirawoot
- PHP VIP Members
- Posts: 3130
- Joined: 17/06/2019 10:30 am
Re: การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
ลองอั้นดูครับ ใช้ที่ต้องหรือเปล่า
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)
-
- PHP VIP Members
- Posts: 3029
- Joined: 04/06/2020 10:05 am
Re: การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
ขอบคุณค่ะ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 ยังไงคะพี่
- jirawoot
- PHP VIP Members
- Posts: 3130
- Joined: 17/06/2019 10:30 am
Re: การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ
สร้าง methon post มา ใน class แล้วใช้ request ของ flask ดึงค่าจาก html มา
python
็html
http://flask-classful.teracy.org/
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)
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>
- jirawoot
- PHP VIP Members
- Posts: 3130
- Joined: 17/06/2019 10:30 am
-
- PHP VIP Members
- Posts: 3029
- Joined: 04/06/2020 10:05 am
-
- Similar Topics
- Replies
- Views
- Last post
-
- 0 Replies
- 1057 Views
-
Last post by tatiya
16/08/2018 4:36 pm
-
-
การสืบทอด Class แบบ Object-Oriented หรือ OOP ใน Python
by ธวัชชัย แสนหาญ » 17/11/2018 3:20 pm » in Python Knowledge - 0 Replies
- 1010 Views
-
Last post by ธวัชชัย แสนหาญ
17/11/2018 3:20 pm
-
-
- 2 Replies
- 799 Views
-
Last post by Patipat
13/08/2019 11:31 am
-
-
HTML Input Object สร้างฟอร์ม เพื่อส่งค่าให้ PHP Input object แต่ละประเภท
by samsonnaze3 » 21/03/2012 3:55 pm » in PHP Knowledge - 13 Replies
- 43703 Views
-
Last post by tongclub48
06/05/2015 3:25 pm
-
-
- 8 Replies
- 2445 Views
-
Last post by bolue
09/06/2020 10:22 am
Who is online
Users browsing this forum: No registered users and 6 guests