การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ

Re: การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ

โดย MBMoo » 13/06/2020 10:34 am

ขอบคุณค่ะ

Re: การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ

โดย jirawoot » 12/06/2020 6:23 pm

สร้าง methon post มา ใน class แล้วใช้ request ของ flask ดึงค่าจาก html มา
python

โค้ด: เลือกทั้งหมด

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

โค้ด: เลือกทั้งหมด

<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 รูปแบบเป็นแบบไหนคะ

โดย MBMoo » 12/06/2020 5:29 pm

jirawoot เขียน: 12/06/2020 4:46 pm ลองอั้นดูครับ ใช้ที่ต้องหรือเปล่า

โค้ด: เลือกทั้งหมด

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 รูปแบบเป็นแบบไหนคะ

โดย jirawoot » 12/06/2020 4:46 pm

ลองอั้นดูครับ ใช้ที่ต้องหรือเปล่า

โค้ด: เลือกทั้งหมด

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)

การเขียน class object ของ python และให้ return เรียกใช้ บน html รูปแบบเป็นแบบไหนคะ

โดย MBMoo » 12/06/2020 1:07 pm

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

ข้างบน