15 กรกฎาคม 2560

Flask : static files

ในเวป บางทีเราจำเป็นต้องมีการใช้งานพวก CSS , javascript ที่ส่วนใหญ่ไม่ค่อยมีการเปลี่ยนแปลงบ่อยๆ ซึ่งเรียกรวมๆว่า static file โดย Flask รองรับไฟล์ประเภทนี้ และ จัดเก็บไว้ภายใต้ directory static

/Application folder
   |-app.py
   |/templates
      |-hello.html
   |/static
      |-hello.js
      |-hello.css

วิธีการเรียกใช้งานใน html
<html>

   <head>
      <script type = "text/javascript" 
         src = "{{ url_for('static', filename = hello.js') }}" ></script>
   </head>
   
   <body>
      <input type = "button" onclick = "sayHello()" value = "Say Hello" />
   </body>
   
</html>












ไม่มีความคิดเห็น:

แสดงความคิดเห็น