เริ่มพัฒนา Web Application กับภาษา Python ด้วย Django Framework

 

เริ่มพัฒนา Web Application กับภาษา Python ด้วย Django Framework

Wasin Thiengkunakrit
Oct 7, 2017 · 12 min read
Image for post
Image for post
Web Frameworks Popularity Ranking อ้างอิงจาก www.hotframeworks.com

การติดตั้ง Python และ Django

Image for post
ตัวอย่าง Python3 console
Image for post
ตัวอย่างการติดตั้ง Django
Image for post
ตัวอย่างการเรียกคำสั่ง django-admin เพื่อแสดง Django version ที่ติดตั้ง
Image for post
ตัวอย่างการเรียกคำสั่ง django-admin บน Linux

สร้าง Django Project

Image for post
โครงสร้างโฟลเดอร์ของ Django Project
Image for post
ตัวอย่างการรัน Django project ครั้งแรก
Image for post
คำสั่งที่ใช้สั่งรัน Django Project
Image for post
หน้า web ที่เห็นหลังจากสร้าง Django project และรันสำเร็จ

สร้าง Django Application

Image for post
โครงสร้างโฟลเดอร์ของ Django Application
Image for post
ตัวอย่างไฟล์ views.py
Image for post
ตัวอย่างไฟล์ urls.py
Image for post
ตัวอย่างหน้า Web Page แรก
Image for post
การทำงานของ Django เมื่อได้รับ HTTP Request จาก Web Browser
Image for post
อธิบายการทำงานของ urls.py
Image for post
อธิบายการทำงานไฟล์ views.py

Django Template

Image for post
ตัวอย่างอธิบายการทำงานของ Django Template
Image for post
สร้างโฟลเดอร์ชื่อ templates และสร้างไฟล์ index.html ในนั้น
Image for post
code ในไฟล์ index.html
Image for post
ไฟล์ settings.py ที่ถูกแก้ไขเพื่อให้ Django รู้จัก Folder Template
Image for post
ไฟล์ views.py ที่ใช้ในการโหลด HTML Template ชื่อ index.html แล้วผ่านค่าตัวแปร var1 ลงไป
Image for post
ตัวอย่างผลลัพธ์ของหน้าจอ HTML
Image for post
การเรียกใช้ฟังก์ชั่น render เพื่อย่อ code ที่ใช้ในการ load Django Template
Image for post
Configuration ที่ทำให้เกิด Feature Static
Image for post
สร้างโฟลเดอร์ static และ style.css
Image for post
แก้ไขไฟล์ style.css โดยเพิ่มข้อความดังต่อไปนี้ (เป็นการเปลี่ยนพื้นหลังให้เป็นสีเหลือง)
Image for post
Image for post
ภาพผลลัพธ์การ load static ไฟล์จาก Django

การเชื่อมต่อฐานข้อมูล

Image for post
ไฟล์ settings.py แสดงการตั้งค่าฐานข้อมูล SQLite3 ซึ่งติดตั้งมาเป็นค่า default
Image for post
ตัวอย่างฐานข้อมูลที่ต้องการสร้าง
Image for post
ตัวอย่างไฟล์ models.py สำหรับสร้างฐานข้อมูลที่มี table ชื่อ Book และ Category
Image for post
เพิ่ม Django Application ให้กับ Django Project รู้จัก
Image for post
การทำงานของ Django ORM เพื่อใช้สร้าง Database Schema
Image for post
ตัวอย่างการรันคำสั่ง makemigrations
Image for post
Image for post
ตัวอย่างการรันคำสั่ง migrate เพื่อสร้างฐานข้อมูล
Image for post
โครงสร้างฐานข้อมูลที่ถูกสร้างขึ้นโดย Django

Django Shell

Image for post
ตัวอย่าง Django Shell
Image for post
แสดงขั้นตอนการสร้าง Category Horror ในฐานข้อมูล
Image for post
ทดสอบ Browse ฐานข้อมูลจริงเพื่อดูข้อมูลในฐานข้อมูล
Image for post
ผลลัพธ์การดึง Category ออกจากฐานข้อมูล
Image for post
Image for post
แก้ไขไฟล์ index.html เพื่อแสดงผลการนับ Category object และ Book object จากฐานข้อมูล
Image for post
ไฟล์ views.py ที่แก้ไขเพื่อแสดงค่าการนับ Category และ Book ในฐานข้อมูล
Image for post
Image for post

Django admin site

Image for post
ไฟล์ settings.py โดยปกติค่าเหล่านี้จะใส่มาให้โดย default อยู่แล้ว
Image for post
ไฟล์ urls.py แก้ไขโดยเพิ่มบรรทัดที่ highlight ด้วยกรอบสีแดงทั้งสองบรรทัด
Image for post
Image for post
การสร้าง Super User เพื่อเข้าถึงหน้า Django admin site
Image for post
Image for post
Image for post
แก้ไขไฟล์ admin.py โดยเพิ่ม สองบรรทัดนี้เข้าไป
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

codeburst

Bursts of code to power through your day.

Wasin Thiengkunakrit

WRITTEN BY

codeburst

codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

สอนเขียน Python เพื่อใช้ SSH ด้วย Paramiko และ Netmiko แบบรวบรัด