rate page(เก่า)
rate page(ใหม่)




<html>2.จากนั้นเราจะต้องมี method callInsertType ที่เรียกหน้า template insertType ขึ้นมา และเราจะ
<h1 style ="text-align:center">Add Income Type</h1>
<form action="{% url 'ManMon:saveType'%}" method="post">
<input type="radio" name="choice" value="income">
<label style ="color:rgb(110, 101, 252);">Income</label>
<input type="radio" name="choice" value="payment">
<label style ="color:rgb(110, 101, 252);">Payment</label><br>
{% csrf_token %}
<p style ="color:rgb(110, 101, 252);">
Type:<input type="text" name="type">
<input type="submit">
</p>
</form>
<a href="{% url 'ManMon:mainpage' %}" class="button"> back to main page</a>
</html>
def callInsertType(request): return render(request,"ManMon/insertType.html",'')
3.เมื่อเราเก็บประเภทของรายการเสร็จแล้ว เราจะให้แสดงหน้าให้ผู้ใช้เห็นว่า ผู้ใช้ได้เซฟdef saveType(request): if(request.POST['choice'] == 'income' ): try: save_type = request.POST['type'] except: save_type = "" else: save_type = TypeIncome(type_income = save_type) save_type.save() if(request.POST['choice'] == 'payment' ): try: save_type = request.POST['type'] except: save_type = "" else: save_type = TypePayment(type_payment = save_type) save_type.save() return render(request, "ManMon/saveType.html",{'save_type':save_type})
<html><h1>You save:{{save_type}}</h1><a href="{% url 'ManMon:mainpage' %}" > back to main page</a></body></html>
<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>
จากโค้ดด้านบนจะมีการเลือก 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})
<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>
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})
url(r'^history$', views.history, name="history")จากนั้น เพิ่มปุ่มกดแบบ button ลงไป
<a href="{% url 'ManMon:history' %}">{{"history"}}</a>
<html>จากโค้ดด้านบน จะมี input 5 ตัว คือ
<h1 style = "text-align: center;">Manage Your Money</h1>
<form action="{% url 'ManMon:saveAccount' %}" method="post">
{% csrf_token %}
<p>
Note:
<input type="text" name="note"></p>
<p>Money:
<input type="number" name="money" step="any"></p>
<p>Date:
<input type="datetime-local" name="dmy"></p><br>
<input type="radio" name="choice" value="income">
<label style = "color:rgb(110, 101, 252);" >income</label>
<p>Type:
<select name="type">
{% for typeincome in type_in_list %}
<option for="type_income{{ forloop.counter }}">{{ typeincome.type_income }}</option>
{% endfor %}
</select>
</p><br>
<input type="radio" name="choice" value="payment">
<label style = "color:rgb(110, 101, 252);">payment</label>
<p>Type:
<select name="type">
{% for typepayment in type_pay_list %}
<option for="type_payment{{ forloop.counter }}">{{ typepayment.type_payment }}</option>
{% endfor %}
</select>
</p>
<input type="submit" >
</form>
</html>
def callAccountInput(request):3.ไปที่ไฟล์ ManMon/urls.py แล้วใส่ url ลงไปตามนี้
type_in_list = TypeIncome.objects.order_by('-type_income')
type_pay_list = TypePayment.objects.order_by('-type_payment')
return render(request, 'ManMon/accountInput.html', {'type_in_list':type_in_list,'type_pay_list':type_pay_list})
url(r'^accountInput$', views.callAccountInput, name="callAccountInput")จากนั้น ให้กลับไปที่ไฟล์ ManMon/templates/ManMon/main.html แล้วเพิ่มโค้ดนี้เข้าไป
<a href="{% url 'ManMon:callAccountInput' %}">{{"Account Input"}}</a>ผลคือเราจะได้เป็นปุ่มที่เราจะสามารถเข้าไปที่เราจะสามารถกรอกข้อมูลได้
def saveAccount(request):ซึ่งเราจะให้ POST ดึงค่าที่ได้จาก templates ที่ได้จากการ submit มา แล้วนำไปเก็บไว้ใน
try:
note = request.POST['note']
money = request.POST['money']
type_note = request.POST['type']
date = request.POST['dmy']
except:
note = ""
money = ""
type_note = ""
date = ""
if(request.POST['choice'] == 'payment'):
money = (-1)*float(money)
ac = Account(note = note, money = money, type_note = type_note, pub_date = date)
ac.save()
return render(request, 'ManMon/saveAccount.html',{'note':note, 'money':money, 'type_note':type_note, 'date':date})
url(r'^accountInput$', views.callAccountInput, name="callAccountInput")6.ในส่วนนี้จะเป็นการบอก ผู้ใช้ว่า ผู้ใช้ได้ทำรายการอะไรไปแล้ว โดยจะได้รับค่าจาก method
<html>
<h1 style = "text-align:center; font-size:330%;">Manage Your Money</h1>
<h1>You're already save:</h1>
<p>Note : {{note}}</p>
<p>Type : {{note_type}}</p>
<p>Money : {{money}}</p>
<p>Date : {{date}}</p>
<a href="{% url 'ManMon:mainpage'%}" class="circleBtn">{{"back to main page"}}</a>
</html>
4.หลังจากนั้น เราก็เพิ่ม ManMon/url.py แล้วใส่โค้ดด้านล่างfrom django.shortcuts import render from django.http import HttpResponse def callMainPage(request): return HttpResponse("Hello, this is ManMon app")
เพื่อให้เป็นการเรียกใช้ หน้าแรกของ app ตาม pattern regular expression และเรียกใช้ methodfrom django.conf.urls import url from . import views app_name = 'ManMon' urlpatterns = [ url(r'^$', views.callMainPage, name="callMainPage"), ]
class Account(models.Model): note = models.CharField(max_length=200) money = models.FloatField(default=0) type_note = models.CharField(max_length=200, default='salary')
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.pub_date
def __str__(self):
return self.type_note
def __flo__(self):
return self.money
def __str__(self): return self.note
class TypeIncome(models.Model):
type_income = models.CharField(max_length=200)
def __str__(self):
return self.type_income
Account - ใช้เก็บข้อมูลรายรับและรายจ่ายของผู้ใช้ ซึ่งจะมี attribute ดังนี้class TypePayment(models.Model): type_payment = models.CharField(max_length=200) def __str__(self): return self.type_payment
'ManMon.apps.ManMon',
เพื่อที่จะอัพเดตของมูลใน database ว่ามีอะไรใน model.py ที่เปลี่ยนไปบ้างpython3 manage.py makemigrations ManMon python3 manage.py sqlmigrate ManMon 0001 python3 manage.py migrate
ในขั้นตอนต่อไป เราจะได้เห็นข้อมูลที่เราใส่ลงไป>>> from polls.models import Account,TypeIncome, TypePayment >>> from django.utils import timezone
>>> ac = Account(note = "ได้เงิน", money = 100, type_note =
"saving", pub_date=timezone.now())
>>> ti = TypeIncome(type_income = "saving")>>> ti.save()
>>> tp = TypePayment(type_payment = "food")
>>> tp.save()