เมื่อวานมีโอกาสได้ให้น็อตเข้ามาช่วย Pair Programming เรื่องการเพิ่ม dropdown ของประเภทบทความฝั่ง Admin เพื่อเลือกประเภทของบทความในขั้นตอนการสร้างบทความ โดยมีประเภท บทความทั่วไป บทความโภชนาการและบทความเภสัชสนเทศ เลยอยากทวนวิธีการที่ได้ทำดังนี้
ส่วน DB
- ไปที่ไฟล์ model.py เพื่อเอาคอมเม้นตรง type = models.ForeignKey ('Type') ออกแล้วเพิ่ม null=True, blank=True ต่อหลัง 'Type' ในวงเล็บ เพื่อเรียกใช้ตาราง Type อีกครั้ง
- เมื่อมีการทำอะไรเกี่ยวกับตารางเช่นการแก้ไขฟิลด์ในตารางให้เราสั่ง ./manage.py evolve --hint เพื่อให้ระบบแจ้งว่าเราต้องทำอะไรต่อไป คือระบบจะแสดงข้อความให้เราสร้างไฟล์ใดๆ ขึ้นมา
- สร้างไฟล์ชื่อ add_type_to_news.py ใน /news/evolutions มีข้อความดังนี้
from django_evolution.mutations import *
from django.db import models
MUTATIONS = [
AddField('News', 'type', models.ForeignKey, null=True, related_model='news.Type')
]
- จากนั้นบันทึกไฟล์และไปเพิ่มชื่อไฟล์ใน _init_.py ใน /news/evolutions
- สั่ง ./manage.py evolve --hint --execute อีกครั้ง ถือเป็นอันเสร็จการจัดการ DB
- ในขั้นตอนนี้เราจะได้ dropdown ของประเภทของบทความในฝั่งแอดมิน ในหน้าการเพิ่มข่าว โดยเราสามารถเพิ่มแก้ไขประเภทได้ที่ได้ที่ Type
ขั้นตอนการดึงข้อมูลจากฐานข้อมูลออกมาใช้งานในฝั่ง Templates
- เข้าไปที่ไฟล์ view.py ใน /news/
- แก้ไขดังนี้
def list_articles(request):
n = News.objects.filter(published_at__lte=datetime.now()).filter(status=2, article=True)
t = Type.objects.all().order_by('priority') --> ดึงข้อมูลทั้งหมดในตาราง Type โดยเรียงตาม priority
is_article = True -->เอาไว้เช็คว่าเป็นบทความนะctx = {'title': _('Recent Articles'),
'is_article': is_article,
'type': t,
'letters': string.uppercase,
'thai_letters': [unichr(i) for i in range(0x0E01, 0x0E2F) + range(0x0E40, 0x0E44)],
}
return object_list(request, queryset=n, extra_context=ctx,
paginate_by=ITEMS_PER_PAGE)
def show_articles_by_type(request,type):
c = Type.objects.filter(slug=type)
t = Type.objects.all().order_by('priority')
is_article = True
if c:
n = News.objects.filter(published_at__lte=datetime.now()).filter(type__in=c, status=2, article=True)
ctx = {'title': _('Articles') +' : ' + c[0].name,
'this-category': type,
'is_article': is_article,
'type': t,
'letters': string.uppercase,
'thai_letters': [unichr(i) for i in range(0x0E01, 0x0E2F) + range(0x0E40, 0x0E44)],
}
return object_list(request, queryset=n, extra_context=ctx,
paginate_by=ITEMS_PER_PAGE)
else:
raise Http404
การจัดการส่วน Templates
- สร้างไฟล์ _articles_categorise.html
<div class="categories">
<ul class="category-list">
{% for category in type %}
{% ifequal this_category category.slug %}
<li class="this-category"><a href="/articles/{{ category.slug }}">{{ category.name }}</a></li>
{% else %}
<li><a href="/articles/{{ category.slug }}">{{ category.name }}</a></li>
{% endifequal %}
{% endfor %}
<li><a href="/scims/articles/">{{_("Pharmaceutical Articles") }}</a></li>
</ul>
</div>
- แก้ไขไฟล์ใน base_index.html เพิ่มส่วนนี้ลงไปตรง <div id="bd-content"> เพื่อเช็คว่าเป็นบทความหรือไม่ ถ้าใช่ตรงส่วนของ categories-top จะแสดงเมนูย่อยของบทความสุขภาพ ถ้าไม่ใช้แสดงเมนูย่อยของข่าว
{% if is_article %}
{% include "_articles_categories.html" %}
{% else %}
{% include "_categories.html" %}
{% endif %}
- จากนั้นลอง start server แลัวรันดูผล
วิธีการประมาณนี้ที่จำได้ 55+ วิธีที่ว่ามานี่ไม่ค่อยละเอียดหรอกนะคะ แค่บันทึกไว้เท่าที่จำได้