สร้าง view สำหรับเป็นหน้า homepage
โดยสร้างไฟล์ views.py ใน /mysite และเขียนโค้ดดังนี้
from django.template import loader
from django.shortcuts import render,get_object_or_404
from django.views import generic
def index(request):
return render(request, 'homepage/index.html')
สร้าง directory ชื่อ homepage ใน /polls/template
จากนั้นสร้าง template ชื่อ index.html ใน /polls/template/homepage
มีโค้ด html ดังนี้
<html>
<head>
<title>Polls Application</title>
</head>
<body>
Homepage
<br><br>
<a href='/polls/'>Polls Application</a> <br><br>
<a href='/admin/'>Admin Setting</a>
</body>
</html>
จากนั้น ตั้งค่า URL ให้เมื่อเข้ามาหน้าแรก จะเจอหน้า homepage ที่เราสร้างไว้
โดยแก้ไขในไฟล์ urls.py ใน /mysite
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
url(r'^$', views.index , name='index'),
]
ก็จะได้หน้า homepage เป็นแบบนี้
ปัญหาที่พบ
1. พยายามที่จะทำเป็น generic views โดยสร้าง class IndexView ดังนี้
class IndexView(generic.DetailView):
template_name = 'homepage/index.html'
และแก้ไข URL เป็นดังนี้
url(r'^$', views.IndexView.as_view() , name='index'),
แต่ไม่สามารถทำได้ เนื่องจากขึ้น Error ดังนี้
2. Template อยู่ในที่ๆไม่น่าจะอยู่
ตอนแรกผมพยายามนำ template ไว้ที่ /templates/homepage และ /mysite/templates/homepage แต่กลับขึ้น Error ว่าหา template ไม่เจอ
ผมจึงแก้ปัญหาด้วยการนำ template ไปไว้ใน /polls/templates/homepage จึงจะสามารถหา template เจอ
Ex.2 - Create a view that shows all questions and results
ผมจะสร้าง view ขึ้นมาใหม่ โดยตั้งชื่อว่า AllResultView
เพิ่มเติมในไฟล์ views.py ใน /polls ดังนี้
class IndexView(generic.ListView):
template_name = 'polls/allresult.html'
context_object_name = 'all_question_list'
def get_queryset(self):
return Question.objects.all()
จากนั้นผมก็สร้าง template ชื่อ allresult.html ใน /polls/templates/polls/
{% if all_question_list %}
{% for question in all_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% for choice in question.choice_set.all %}
{{ choice.choice_text }} - {{ choice.votes }} Votes
<br>
{% endfor %}
<br>
{% endfor %}
{% else %}
<p>No polls are available.</p>
{% endif %}
จากนั้นก็ทำการตั้งค่า URL ให้ view นี้
โดยการเพิ่มใน urls.py ใน /polls
url(r'^allresult/', views.AllResultView.as_view(), name='allresult'),
และเนื่องจากผมต้องการให้หน้าแสดงผลลัพธ์ทั้งหมด สามารถลิ้งมาจาก IndexView ของ polls app ได้ ผมจึงไปแก้ไข template ของ IndexView ใน /polls/templates/polls/index.html
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
<br><br><a href="{% url 'polls:allresult' %}">View All Results</a>
{% else %}
<p>No polls are available.</p>
{% endif %}
Ex.3 - Load data from csv file (local file, submitted file)
ตอนนี้ผมทำแค่แต่แบบ local file
สร้างไฟล์ csv ขึ้นมาเก็บข้อมูล Question , Choice และ Vote
โดยตั้งชื่อไฟล์ว่า question_data.csv นำไปเก็บไว้ที่ /polls
จากนั้นทำการสร้าง view ที่ไว้จัดการกับ csv file ใน /polls/views.py
โดยตั้งชื่อ view ว่า loadCSV
def loadCSV(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()
with open('polls/question_data.csv',encoding='utf-8') as csvfile:
dataReader = csv.DictReader(csvfile)
question = None
for row in dataReader:
if row["Question"] != "" and row["Choice"] == "":
question = Question(question_text = row["Question"] , pub_date=timezone.now())
question.save()
elif row["Question"] == "" and row["Choice"] != "":
question.choice_set.create(choice_text = row["Choice"] , votes = int(row["Vote"]))
return HttpResponseRedirect(reverse('polls:index'))
จากนั้นทำการเพิ่ม URL ให้ view นี้ โดยแก้ไขไฟล์ urls.py ใน /polls
url(r'^loadcsv/', views.loadCSV, name='loadcsv'),
ผมต้องการให้มีปุ่มกด load CSV ในหน้า admin แต่เนื่องจากผมทำไม่เป็น จึงใช้การจำ URL และใส่ในช่อง address
ก่อน load csv
หลังจาก load csv โดยการใส่ address เป็น http://127.0.0.1:8000/polls/loadcsv
ไม่มีความคิดเห็น:
แสดงความคิดเห็น