วันอังคารที่ 3 ธันวาคม พ.ศ. 2556

Djangobook Chapter 3: Views and URLconfs

Chapter 3: Views and URLconfs
ในการทดลองการใช้ Django Dynamic URLs ได้ import datetime ซึ้งเป็น library ของ python มาใช้แสดงเวลาปัจจุบันมีการส่ง request จาก URL เพื่อนำเวลามาแสดงผลโดยจะเป็นการบวกเวลาเพิ่มจากปัจจุปัน 
 Show current time
ในหน้า home ของเวปนั้นจะให้เรียกใช้ function home ใน views.py เพื่อแสดงข้อความและมีลิงค์ให้คลิกแสดงเวลาปัจจุบัน ส่วนของ URL จะเพิ่ม URL ที่เรียกใช้ function home เข้าไปในส่วน urls.py 
urls.py 
 url(r'^$', 'mysite.views.home'),  
views.py 
 def home(request):  
   html = """<h1>This is home </h1> <br>  
       <a href= time/ > Click for watch current datetime </a>"""  
   return HttpResponse(html)   


urls.py 
 url(r'^time/$', 'mysite.views.current_datetime'),  
 views.py 
 def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body><h2><center>It is now %s.</h2></body></html>" % now
    return HttpResponse(html)  

เมื่อมีการคลิกที่ลิงค์ Click for watch current datetime จากลิงค์นี้ก็จะมีการเชื่อมต่อไปยัง http://127.0.0.1:8000/time/ ซึ่งใน urls.py URL นี้จะส่ง request ต่อไปยัง function current_datetime ใน views.py function นี้จะทำการรับวันที่และเวลาปัจจุบัน จาก datetime.now() ทำการรีเทิร์นเป็น html แสดงวันที่และเวลาดังรูปด้านล่าง




 Show hours ahead
จะเป็นการใช้ประโยชน์จาก URL สามารถนำมาใช้เป็น offset เพื่อนำไปบวกเพิ่มเวลาโดยจะมี Regular Expressions ในการกำหนดเงื่อนไขที่จะรับข้อมูลตามตารางด้านล่าง
Symbol Matches
. (dot) Any single character
\d Any single digit
[A-Z] Any character between A and Z (uppercase)
[a-z] Any character between a and z (lowercase)
[A-Za-z] Any character between a and z (case-insensitive)
+ One or more of the previous expression (e.g., \d+ matches one or more digits)
[^/]+ One or more characters until (and not including) a forward slash
? Zero or one of the previous expression (e.g., \d? matches zero or one digits)
* Zero or more of the previous expression (e.g., \d* matches zero, one or more than one digit)
{1,3} Between one and three (inclusive) of the previous expression (e.g., \d{1,3} matches one, two or three digits)


ในส่วนของ urls.py จะเพิ่ม URL สำหรับรับข้อมูลโดยจะรับข้อมูลเฉพาะตัวเลขมี รับจำนวนได้สูงสุดสองหลัก แล้วส่ง request ต่อให้ function hours_ahead_Er ใน views.py
 url(r'^time/plus_Er/(\d{1,2})/$', 'mysite.views.hours_ahead_Er'),  
Function hours_ahead_Er ใน views.py จะทำการรับ request กับ offset จากนั้นจะนำมา cast เป็น integer กรณีเกิด Error จะแสดงหน้า Http404() จากนั้นจะนำ offset มาบวกกับวันเวลาปัจจุบันเพื่อแสดงชั่วโมงข้างหน้าและรีเทิร์นเป็น html
 def hours_ahead_Er(request, offset):  
   try:  
     offset = int(offset)  
   except ValueError:  
     raise Http404()  
   dt = datetime.datetime.now() + datetime.timedelta(hours=offset)  
   html = "<html><body><center>In %s hour(s), it will be %s.</body></html>" % (offset, dt)  
   return HttpResponse(html)  




Code for Basic Django Dynamic URLs
urls.py

views.py
Reference
http://www.djangobook.com/en/2.0/chapter03.html

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

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