การกำหนด URL Patterns บน Django

หลังจากติดตั้ง Django บน IIS  ต่อไปก็กำหนด routes ให้กับ django project ของเรา

1 สร้าง views.py

สร้าง function hello ดังนี้

2 ไฟล์ urls.py

import function hello เข้าไปใช้งาน  และกำหนด url

r  คือการใช้งาน regular expression

^hello/$ คือรูปแบบ ขึ้นต้นด้วยคำว่า  hello และ  / เป็นเครื่องหมายสุดท้าย

 

ต้องการ url profile พร้อมส่งค่าเป็นตัวอักษรไป

http://127.0.0.1:8000/profile/chaichon

http://127.0.0.1:8000/profile/thailand

สร้าง function  profile ขึ้นมา

ผลลัพธ์

 

ส่วน Regular expressions เบื้องต้นก็ตามนี้ครับ

 

มีชุดตัวอักษรด้วยครับ
\d  any decimal digit               => [0-9]
\D  non-digit character             => [^0-9]
\s  any whitespace character        => [\t\n\r\f\v]
\S  non-whitespace character        => [^ \t\n\r\f\v]
\w  any alphanumeric character      => [a-zA-Z0-9_]
\W  any non-alphanumeric character  => [^a-zA-Z0-9_]

 

ดูเพิ่ม  List of Useful URL Patterns

 

จบแล้วครับ หวังว่าจะมีประโยชน์นะครับ