ในหน้าหลักนั้นเราใส่ให้เราสามารถดูได้แค่ 5 รายการแรก แต่ในส่วนนี้นั้นเราจะมาสอน
คนก่อน1.เราก็สร้างหน้า template ของ history เพื่อให้เราสามารถดูรายการเก่าๆได้ตามนี้
<html>
<h1 style="text-align:center">History</h1>
{% if account_list %}
<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 : {{ money_sum }} Baths</td>
</tr>
<a href="{% url 'ManMon:mainpage'%}">{{"back to main page"}}</a>
{% else %}
<p> Insert your income</p>
{% endif %}
</html>
2.จากนั้นเพิ่ม method ที่ใช้ในการเรียก
def history(request):ในส่วนก็จะคำนวณค่ายอดเงินรวม และค่าทั้งหมดไปที่หน้า template history
account_list = Account.objects.order_by('-pub_date')
money_sum = 0
for account in account_list :
money_sum += account.money
for account in account_list :
if account.money < 0 :
money = account.money
account.money = (-1)*float(money)
return render(request, 'ManMon/history.html', {'account_list':account_list, 'money_sum':money_sum})
3.เปิดไฟล์ ManMon/urls.py แล้วเพิ่ม url ของ history ตามนี้
url(r'^history$', views.history, name="history")จากนั้น เพิ่มปุ่มกดแบบ button ลงไป
<a href="{% url 'ManMon:history' %}">{{"history"}}</a>
ไม่มีความคิดเห็น:
แสดงความคิดเห็น