ใน tutorial นี้ เราจะนำรายการที่เราบันทึกไว้ มาแสดงออกมานะครับ
1.เราจะสร้างไฟล์ ManMon/template/ManMon/main.html ซึ่งเป็นไฟล์ template ที่เราจะใช้เพื่อนำมาแสดงผลข้อมูลตามที่เราต้องการ แล้วก็ใส่โค้ดด้านล่างลงไป
<html><h1 style = "text-align: center;">Manage Your Money</h1> <table border=1 style="width:50%;clear:both" align="center"> <tr> <td>Note</td> <td>Money</td> <td>Type</td> <td>Date</td> </tr> {% for account in account_list %} <tr> <td>{{ account.note }}</td> <td>{{ account.money }} Baths</td> <td>{{ account.type_note }}</td> <td>{{ account.pub_date }}</td> </tr> {% endfor %} <tr> <td style="text-align: center;" colspan="5">money sum : {{ remain_money }} Baths</td> </tr></html>
2.จาก tutorial 1 เราจะเปลี่ยน method callMainPage ให้เป็นตามด้านล่าง เพื่อให้มันดึง
template ที่เราต้องการมาใช้
จากโค้ดด้านบนจะมีการเลือก 5 รายการล่าสุดที่มาแสดง ที่หน้าหลักdef callMainPage(request):
account_list = Account.objects.order_by('-pub_date')
#calulation the rest of money
remain_money = 0
for account in account_list :
remain_money += account.money
account_list = Account.objects.order_by('-pub_date')[:5]
for account in account_list :
if account.money < 0 :
money = account.money
account.money = (-1)*float(money)
return render(request, 'ManMon/main.html',
{'remain_money':remain_money, 'account_list':account_list})
ไม่มีความคิดเห็น:
แสดงความคิดเห็น