วันเสาร์ที่ 6 กุมภาพันธ์ พ.ศ. 2559

Exercise

Exercise 

  • Create homepage at http://localhost:8000
  • Create a view that shows all questions and results
  • Load data from csv file

Create homepage at http://localhost:8000

     ทดลองเข้าที่ http://localhost:8000 จะเห็นดั้งรูปเพราะยังไม่ได้สร้างหน้าเเรกน่ะครับ เเต่เราสามารถที่จะเข้าไปยังหน้า http://localhost:8000/polls ได้เพราะเราได้ทำการสร้างมาก่อนหน้านี้เเล้วเพราะฉนั้นเรามาสร้างหน้า homepage กันดีกว่า
  
ภาพ(http://localhost:8000)

1. ทำการสร้าง app ขึ้นมาใหม่ 1 app ชื่อว่า home เพื่อเป็นตัวกลางในการเรียกใช้งาน app อื่นๆๆ โดยใช้คำสั่งผ่าน Terminal ที่ directory งานของเราครับ
 
  $ python3 manage.py startapp home
 

1.1 จากนั้นทำการเเก้ไขไฟล์ mysite/settings.py ตรง 
 
   INSTALLED_APPS = [
        'home.apps.HomeConfig', # เพิ่ม app home ที่เรา สร้างใหม่เข้าไป
        'polls.apps.PollsConfig',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
 
1.2 ทำการเเก้ไขไฟล์ mysite/urls.py ตรง 
 
    urlpatterns = [
        url(r'', include('home.urls')), # เพิ่ม url app home ที่เราสร้างใหม่เข้าไป               
        url(r'^polls/', include('polls.urls')),
        url(r'^admin/', admin.site.urls),
    ]
 
  • url(r'', include('home.urls')) บอกว่าถ้า url ที่ใส่เข้าไปเป็น url ว่างๆให้ไปใช้ไฟล์ urls ที่ home นั้นคือ http://localhost:8000
  • url(r'^polls/', include('polls.urls')) บอกว่าถ้า url ที่ใส่เข้าไปเป็น url ที่ตามด้วย /polls ว่างๆให้ไปใช้ ไฟล์ urls ที่ polls นั้นคือ http://localhost:8000/polls
  • url(r'^admin/', admin.site.urls) บอกว่าถ้า url ที่ใส่เข้าไปเป็น url ที่ตามด้วย /admin ให้ไปยังหน้า admin  
 -------------------
1.3 ทำการเเก้ไขไฟล์ home/urls.py
 
    from django.conf.urls import url
    from . import views
    app_name = 'home'
    urlpatterns = [
        url(r'^$', views.index, name='index')
    ]
 
  •   url(r'^$', views.index, name='index') ถ้า url ว่างๆให้ใช้งานไฟล์ views ดูที่ index
 -------------------
1.4 ทำการเเก้ไขไฟล์ home/views.py
        
    from django.shortcuts import render
    def index(request):
        return render(request, 'home/index.html')
    
  •  ให้ render ไฟล์ home/index.html
-------------------
1.4 ทำการสร้างไฟล์ home/index.html
        
<html>
 <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Home</title></head>
 <body>
  <div align="center"><h3>Django Tutorials Polls</h3><hr/></div>
   <center><table border ="0">
   <tr>
    <td width="260" align="center">
     <a href="/admin"><strong>Administrator</strong></a><br>
    </td>
    <td width="260" align="center">
     <a href="/polls"><strong>Question List Polls</strong></a><br>
    </td>  
                <td width="260" align="center">
     <a href=""><strong>Question List Update</strong></a><br>
    </td>
    <td width="260" align="center">
     <a href=""><strong>View All Vote</strong></a><br>
    </td >  
   </tr>
 </body>
</html>
    
 จากนั้นเข้าไปที่ http://localhost:8000 เราก็จะได้หน้า homepage ของเรามาดั้งรูปครับ
ภาพ(http://localhost:8000 หลังจากที่ทำตามขั้นตอนดั้งกล่าว)
  • Administrator เข้าไปยังหน้า admin
  • Question List Polls เข้าไปยังหน้า polls เพื่อทำการโหวด
  • Question List Update เข้าไปยังหน้าของ Update คำถามด้วย csv ตาม Exercise ข้อ 3
  • View All Vote เข้าไปยังหน้า เเสดงคำถาม เเละ การโหวดทั้งหมด ตาม Exercise ข้อ 2

Create a view that shows all questions and results

ภาพ (http://localhost:8000/polls/) 

ภาพ (http://localhost:8000/polls/1/)
ภาพ (http://localhost:8000/polls/1/results/)
 1.ทำการเเก้ไขไฟล์ polls/views.py เพิ่ม Function "indexviewall" เพื่อทำการเเสดงผลของคำถาม เเละการโหวดทั้งหมด
        
 def indexviewall(request):
    question_list = Question.objects.all()
    sumvote = {}
    savesum = 0
    for question in question_list:
        choice_list = question.choice_set.all()
        for choice in choice_list:
            savesum = savesum+int(choice.votes)
        sumvote[str(question)]=savesum
        savesum = 0
    return render(request, 'polls/view-all.html', {'question_list': question_list,'sumvote': sumvote})
   
 2.ทำการสร้างไฟล์ view-all.html ใน templates เพื่อทำการเเสดงผลของข้อมูล
        
 <title>View All Vote</title>
 <div align="center"><h1>View All Vote</h1><hr/>
 {% if question_list %}
  {% for question in question_list %}
  <table border="1" width="40%"> 
  <tr>
  <td colspan="3" bgcolor="#CC00FF" align="center">{{ question.question_text }}</td>
  </tr>
   {% for choice in question.choice_set.all %}
   <tr>
    <td th width="70%">&nbsp;{{ choice.choice_text }}</td>
    <td th width="15%" align="right">{{ choice.votes }}&nbsp;</td>
    <td th width="15%" align="center">vote{{ choice.votes|pluralize }}</td>
   </tr>
   {% endfor %}

                        {% for key, value in sumvote.items %}
                  {% if question.question_text == key %}
     <tr><td colspan="3" bgcolor="#CC66FF" align="center">จำนวนโหวดทั้งหมด = {{value}}</td></tr>
    {% endif %}
   {% endfor %}

   </table><br/>
  {% endfor %}
 {% else %}
  <p>No polls are available.</p>
 {% endif %}
<hr/><a href="/"><button>Home</button></a></div>
</div>
  

 Load data from csv file

1.ทำการเเก้ไขไฟล์ polls/views.py เพิ่ม Function "updatecsvnew" กับ "updatecsvadd" เพื่อทำการโหลดคำถามเเละตัวเลือกจากไฟล์ csv โดยที่ updatecsvnew เป็นการลบคำถามเก่าเเล้วใส่คำถามใหม่ไปเเทน เเต่ updatecsvadd เป็นการเพิ่มคำถามใหม่เข้าไป
        
 def updatecsvnew(request):
    question_list = Question.objects.all()
    for question in question_list:
        choice_list = question.choice_set.all()
        for choice in choice_list:
            choice.delete()
        question.delete()
    save_csv = []
    with open('polls/CSV/Question.csv') as csvfile:
        readCSV = csv.reader(csvfile, delimiter = ',')
        for row in readCSV:
            save_csv.append(row)
        for i in range(1,len(save_csv),1):
            question = Question(question_text = str(save_csv[i][0]) ,pub_date = timezone.now())
            question.save()
            for l in range(1,len(save_csv[i]),1):
                question.choice_set.create(choice_text = str(save_csv[i][l]) , votes = 0)
    return HttpResponseRedirect(reverse('polls:up-date'))

 def updatecsvadd(request):
    question_list = Question.objects.all()
    save_csv = []
    with open('polls/CSV/Question.csv') as csvfile:
        readCSV = csv.reader(csvfile, delimiter = ',')
        for row in readCSV:
            save_csv.append(row)
        for i in range(1,len(save_csv),1):
            question = Question(question_text = str(save_csv[i][0]) ,pub_date = timezone.now())
            question.save()
            for l in range(1,len(save_csv[i]),1):
                question.choice_set.create(choice_text = str(save_csv[i][l]) , votes = 0)
    return HttpResponseRedirect(reverse('polls:up-date'))
           

- - - - - - - - -

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

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