วันอาทิตย์ที่ 19 มีนาคม พ.ศ. 2560

บัญชีรายรับรายจ่าย : tutorial 5

    ในส่วนนี้เราจะทำให้ผู้ใช้สามารถใส่ type income และ type payment ได้
        1.สร้างไฟล์ template insertType เพื่อให้ผู้ใช้สามารถเพื่อประเภทของรายการได้ตามที่
    ต้องการ
<html>
<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>
        2.จากนั้นเราจะต้องมี method callInsertType ที่เรียกหน้า template insertType ขึ้นมา และเราจะ
    ต้องมี method saveType  ที่ใช้เพื่อนำประเภทของรายการไปเก็บ
def callInsertType(request):    return render(request,"ManMon/insertType.html",'')
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})
        3.เมื่อเราเก็บประเภทของรายการเสร็จแล้ว เราจะให้แสดงหน้าให้ผู้ใช้เห็นว่า ผู้ใช้ได้เซฟ
    ประเภทรายการนี้ไปแล้ว
<html><h1>You save:{{save_type}}</h1><a href="{% url 'ManMon:mainpage' %}" > back to main page</a></body></html>

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

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