วันพุธที่ 27 พฤศจิกายน พ.ศ. 2556

Assignment 5 : Django with Template


Building Software System II: Assignment 5 Django with Template
โจทย์กำหนด
  - ใช้ Django ร่วมกับ template แสดงวันที่และเวลาปัจจุบัน
 

 - ใช้ Django และ Python แสดงวันที่และเวลาปัจจุบัน
  ขั้นตอนการทำงาน                            
   1. เปิด Terminal เพื่อทำการสร้าง Project Django ในที่นี้ใช้ชื่อว่า DjangoTestTemplate
โดยใช้คำสั่ง
        django-admin.py startproject DjangoTestTemplate

        หรือ
        django-admin.py startproject name




   2.ใช้คำสั่ง ls เพือตรวจสอบว่า มี directory ที่ชื่อ DjangoTestTemplate ที่ได้สร้างขึ้นอยู่ใน directory ที่ต้องการหรือไม่ ซึ่งจะสร้างขึ้นใน home



   3.เมื่อพบว่า directory ถูกสร้างขึ้นเรียบร้อย ให้เปลี่ยนนี้ใหม่ เพราะว่าในdirectoryนี้มีอีก folder หนึ่ง ซึ่งมีชื่อเหมือนกัน เพื่อป้องกันการสับสนจึงต้องเปลี่ยนชื่อใหม่ จาก DjangoTestTemplate เป็น Assignment5 โดยใช้คำสั่ง
        mv DjangoTestTemplate Assignment5
        หรือ
        mv oldname newname


   4.หลังจากเปลี่ยนชื่อแล้ว ให้เข้าไปภายใน directory ที่เปลี่ยนชื่อนี้ โดยคำสั่ง
        cd Assignment5
        หรือ
        cd directory
        ซึ่งจะพบว่ามี 1 directory ย่อย และ ไฟล์ manage.py

   5.ให้ทำการ run server จากไฟล์ manage.py โดย
        python manage.py runserver


        ทดสอบโดยการ พิมพ์ 127.0.0.1:8000 บน browser


   6.สร้าง folder ชื่อว่า Template ไว้ใน directory ของ project ที่สร้าง


   7.เข้าไปใน directory ย่อยที่ชื่อ DjangoTestTemplate จากนั้นเปิดไฟล์ setting.py และเข้าไปแก้ไขในไฟล์ ดังนี้

   -เพิ่ม code ต่อจาก #Application definition
   TEMPLATE_DIRS = ('/home/jaa/Documents/BuildingSoftwareII/Assignment5/Template',)
   -แก้ไข time zone เป็น  
   TIME_ZONE = 'Asia/Bangkok' 
   
   8.สร้างไฟล์ views.py แล้ว save ไว้ใน directory ย่อยที่อยู่ใน project นี้ เพื่อเอาไว้สร้างฟังก์ชันที่ดึงข้อมูลจาก url (ในที่นี้ลองใช้ฟังก์ชัน แสดงวันและเวลา) มี code ดังนี้
 
from django.template import Template, Context
from django.shortcuts import render
from django.http import HttpResponse
import datetime,time

def display(request):
  now = datetime.datetime.now()
  t = Template("<html><body>It is now {{ current_date }}</body></html>")
  html = t.render(Context({'current_date':now}))
  return HttpResponse(html)

def current_datetime(request)ShowDateTime.html:
  now = datetime.datetime.now()
  return render(request,'CurrentDateTime.html',{'current_date':now})

def displaytime(request):
  i = datetime.datetime.now()
  date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
  today = datetime.date.today().strftime("%d-%B-%Y")
  time = datetime.datetime.now().strftime("%H:%M:%S")
  return render(request,'ShowDateTime.html',{'date':date,'time':time,'now':today})

def plusday(request,d):
  d = int(d)
  i = datetime.datetime.now()
  day = i + datetime.timedelta(days=d)
  return render(request,'PlusDay.html',{'plus_day':d,'day':day})


   9.เปิดไฟล์ url.py ใน directory ย่อยที่อยู่ใน project นี้ แล้วแก้ไขโดยเพิ่ม code ต่อไปนี้
   -เพิ่มต่อจาก code url(r'^admin/', include(admin.site.urls)), ที่มีให้ในไฟล์
    url(r'^date/$','DjangoTestTemplate.views.current_datetime'),
    url(r'^date/plusday/(\d+)/$','DjangoTestTemplate.views.plusday'),
    url(r'^now/$','DjangoTestTemplate.views.displaytime'),

   10. สร้าง html file เก็บไว้ใน folder template 3 ไฟล์เพื่อรองรับการทำงานของฟังก์ชันในไฟล์ views.py โดยใช้ชื่อว่า ShowDateTime.html, CurrentDateTime.html และ PlusDay.html
   - code ในส่วนของ ShowDateTime.html
<html>
<body>
<center>
  <p><b>Today is</b> {{now}} <b>Time</b> {{time}}</p>
  <p><b>Now</b> {{date}} </p>
</center>
</body>
</html>

  - code ในส่วนของ CurrentDateTime.html
<html>
<body>
<center>  
  <p><b>Now</b> {{current_date}} </p>
</center>
</body>
</html>

 - code ในส่วนของ CurrentDateTime.html
<html>
<body>
<center>  
  <p><b>Plus Day</b> {{plus_day}} </p>
  <p><b>Next</b> {{plus_day}} <b> Day is</b> {{day}} </p>
</center>
</body>

</html>

   11.ทดสอบ output โดยการพิมพ์ url บน browser 
   - 127.0.0.1:8000/date/


   - 127.0.0.1:8000/date/plusday/5/


   - 127.0.0.1:8000/date/now/


















วันอาทิตย์ที่ 24 พฤศจิกายน พ.ศ. 2556

Building Software System II: Assignment 4


Building Software System II: Assignment 4
โจทย์กำหนด
  - ใช้ Django และ Python แสดงวันที่และเวลาปัจจุบัน
 

 - ใช้ Django และ Python แสดงวันที่และเวลาปัจจุบัน
  ขั้นตอนการทำงาน                            
   1. สร้าง file .py ชื่อ views.py เพื่อเขียน code เป็น function เพื่อรับและส่งค่าไปแสดงใน page


    การทำงานของ code           
    - import library ต่างๆเข้ามาช่วยในการทำงาน ได้แก่ import datetime และ from django.http import HttpResponse
   
- สร้างฟังก์ชัน เพื่อให้รับค่า url เข้ามา จากนั้นใช้คำสั่ง now = datetime.datetime.now()
เพื่อ เรียก ดูเวลาปัจจุบัน
    - เขียน code html แสดง ค่าเวลาปัจจุบันบน page

   2.เปิดไฟล์ url.py ใน folder ที่อยู่ใน project ที่สร้างขึ้น และเข้าไปแก้ code url ดังนี้
url(r'^current_datetime/$', 'myCalendar.views.current_datetime'), โดยเพิ่มจากบรรทัดสุดท้ายของ code


   3.จะได้ output  ดังนี้













วันพุธที่ 20 พฤศจิกายน พ.ศ. 2556

Building Software System II: Assignment 3

Building Software System II: Assignment 3
โจทย์กำหนด
                        - ใช้ภาษา python เขียน file html แสดง index ของแต่ละ profile และเมื่อ click เข้าไปแล้ว ให้แสดง profile ของแต่ละบุคคล

               ขั้นตอนการทำงาน
                     1. สร้าง file text เพื่อ จัดเก็บข้อมูลของแต่ละคนไว้ โดยแบ่งเป็น ข้อมูลของ 1 คน ต่อ  1 บรรทัด


                      2. สร้าง file .py แล้วเขียน โค้ด เพื่อให้ อ่านข้อมูลจากใน Text File แล้ว print ข้อมูลออกมา ซึ่งหลักการทำงานของ Code คือมีการใช้ คำสั่ง open เพื่อ เปิด file text จากนั้นให้มีการ วน loop for เพื่อให้ อ่านไฟล์ทุกบรรทัด โดยคำสั่ง readlines() และใช้คำสั่ง print


          การทำงานของ code           
                     - เขียน ลักษณะของ page ในรูปแบบ ภาษา html จากนั้น ใช้คำสั่งสร้าง file html โดย htmlFile =open(“ ”, “w+”) และเขียน file html ลงไปโดย htmlFile.write() และรับค่าที่ได้จากการอ่าน text มาแสดงเป็น out put และ บน browser


              2.ลักษนะของ output ที่ได้ในรูปแบบ file html เมื่อทำการ run file python ที่สร้างขึ้น และมี output ของคำสั่ง print ที่แสดงประวัติส่วนตัว


              3.จะได้ output  ของ file html ที่เรียอออกมาโดยการ click ที่ file ดังรูป






Building Software System II: Assignment 2

Building Software System II: Assignment 2
โจทย์กำหนด
  - ใช้ภาษา python เขียน file html แสดง list ของ file ใน directory นั้นๆ
 
- ใช้ภาษา python เขียน file html แสดง ปฏิทิน และมี option เพื่ออำนวยความสะดวก

 - ใช้ภาษา python เขียน file html แสดง list ของ file ใน directory นั้นๆ
  ขั้นตอนการทำงาน                            
   1. สร้าง file .py เพื่อเขียน code ให้สร้าง ไฟล์ html


    การทำงานของ code           
    - import library ต่างๆเข้ามาช่วยในการทำงาน
   
- สร้างฟังก์ชัน เพื่อให้รับค่า Pathname เข้ามา จากนั้นใช้คำสั่ง os.listdir(Pathname) เพื่อให้ list รายชื่อ file ใน directory และเก็บไว้เป็น list



    - เขียน ลักษณะของ page ในรูปแบบ ภาษา html จากนั้น ใช้คำสั่งสร้าง file html โดย htmlFile =open(“ ”, “w+”) และเขียน file html ลงไปโดย htmlFile.write()


   2.ลักษนะของ output ที่ได้ในรูปแบบ file html เมื่อทำการ run file python ที่สร้างขึ้น และมี output ของคำสั่ง print ที่แสดงรายชื่อของไฟล์ใน directory นั้นๆ


   3.จะได้ output  ของ file html ที่เรียอออกมาโดยการ click ที่ file ดังรูป



    4.และเมื่อ click ไปยัง folder ก็จะเข้ามาข้างใน Folder นั้น โดย การแสดงรายชื่อของไฟล์ภายใน Folder เป็นการจัดการโดย browser 


 - ใช้ภาษา python เขียน file html แสดง ปฏิทิน และมี option เพื่ออำนวยความสะดวก       
  ขั้นตอนการทำงาน         
  1. สร้าง file python ขึ้นมา และ import calendar เพื่อช่วยในการเขียนปฏิทิน จากนั้น import  xml.etree.ElementTree as etree เพื่อช่วยในการทำเป็นรูปแบบ ของ html 




  การทำงานของ code          
   - import library ต่างๆเข้ามาช่วยในการทำงาน
  
- เรียกปฏิทิน ในรูปแบบ html โดยคำสั่ง  
calendar.HTMLCalendar(calendar.SUNDAY)               
   - เขียน ลักษณะของ page ในรูปแบบ ภาษา html จากนั้น ใช้คำสั่งสร้าง file html โดย htmlFile =open(“ ”, “w+”) และเขียน file html ลงไปโดย htmlFile.write()


   2. จะได้ output  ของ file html ที่เรียอออกมาโดยการ click ที่ file ดังรูป