![]() |
| หลักการทำงานของ Views และ URLconfs ใน Django |
Views คือ หน้าแสดงผลบนเว็บ เป็นไฟล์ที่กำหนดการแสดงผลลัพธ์บนเว็บเพจ คือไฟล์ View.py ใน Django
ส่วน URLconfs คือ ไฟล์การตั้งค่าลิงค์ของเว็บเพจใน Django คือไฟล์ urls.py
หลักการทำงานของ Views และ URLconfs ใน Django
เมื่อมีการ Http Request เข้าโดยจะผ่าน URLconfs ก่อนแล้วจึงส่งการทำงานตามลิงค์ที่กำหนดไปให้ Views จะทำการประมวลผลด้วย Django + Python ตลอดการทำงานจนเมื่อประมวลผลเสร็จจะส่ง Http Response กลับไปในรูปแบบ HTMLViews ใน Django
View.py เป็นไฟล์ที่ใช้กำหนดค่าในการแสดงผลอกมาบนหน้าเว็บเพจ มาลองสร้าง View กันสร้างแอพก่อน
django-admin.py startapp hello
เสร็จแล้วจะได้โฟลเดอร์ hello เพิ่มขึ้นมา ให้เข้าไปที่ hello แล้วเปิดไฟล์แก้ไข view.py
hello/view.py
from django.shortcuts import render # Create your views here.
ให้เพิ่มโค้ดลงไปดังนี้
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def myfunction(request):
return HttpResponse("Hello World!")
แค่นี้เราก็ได้หน้า view.py แล้วครับ
URLconfs ใน Django
ต่อมาเรามาตั้งค่า URLconfs กัน ให้กลับไปที่หน้าของ project ให้เข้าไปแก้ไขไฟล์ urls.py ใหเพิ่มโค้ดจากfrom django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
..
..
url(r'hello/$',myfunction),
)
เป็น
from django.conf.urls import patterns, include, url
from django.contrib import admin
from hello.views import myfunction
urlpatterns = patterns('',
# Examples:
..
..
url(r'hello/$',myfunction),
)
จะเห็นได้ว่า การประกาศ url มีการใช้ Regular Expressions เข้ามาเกี่ยวข้องด้วย โดยสัญญาณ "^" แทนเริ่ม "$" แทนจบ
ผู้อ่านสามารถศึกษาการใช้งาน Regular Expressions ใน Python ได้ที่ python3.wannaphong.com/2014/10/python-regular-expressions.html
แล้วบันทึกไฟล์ เมื่อผู้อ่านรันเว็บโดยใช้คำสั่ง
python manage.py runserver
เข้าไปที่ http://localhost:8000/hello จะพบกับ
Hello World!
แค่นี้ยังไม่จบครับ เรามาปรับแต่งอะไรเล่น ๆ สนุก ๆ กันต่อครับ ผู้อ่านคงอยากจะสร้าง app เดียว เช่น guidebook โดยเป็นส่วนหนึ่งของเว็บไซต์ ซึ่งมีการทำงานหลายอย่าง หากต้องการสร้าง guidebook คงต้องมีการแบ่ง app กันอีก แต่ไม่จำเป็น เพราะหน้า view.py กับ urls.py ได้อย่างอิสระ โดยทำตามนี้ครับ
เข้าไปแก้ไขที่หน้า view.py ของ hello
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def myfunction(request):
return HttpResponse("Hello World!")
def myfunction_2(request):
return HttpResponse("I am Python! :D")
จะเห็นได้ว่าเราจะเพิ่มคาส ขึ้นมาเรียกาใช้งานเมื่อมีการ request ต่อมาห้กลับไปที่หน้าของ project ให้เข้าไปแก้ไขไฟล์ urls.py ให้ผู้อ่านทำการแก้ไขจาก
from django.conf.urls import patterns, include, url
from django.contrib import admin
from hello.views import myfunction
urlpatterns = patterns('',
# Examples:
..
..
url(r'hello/$',myfunction),
)
เป็น
from django.conf.urls import patterns, include, url
from django.contrib import admin
from hello.views import myfunction
urlpatterns = patterns('',
# Examples:
..
..
url(r'hello/$',hello.views.myfunction),
url(r'who/$',hello.views.myfunction_2),
)
เสร็จแล้วบัทึกแล้วลองรันดู โดยใช้คำสั่ง
python manage.py runserver
เมื่อรันเข้าไปที่ http://localhost:8000/hello จะพบกับ
Hello World!
แต่เมื่อเข้าไปที่ http://localhost:8000/who จะพบกับ
I am Python! :D
ติดตามบทความต่อไปนะครับ
ขอบคุณครับ



0 ความคิดเห็น:
แสดงความคิดเห็น
แสดงความคิดเห็นได้ครับ :)