Commit a59f4b66 authored by Carlo Joseph Echon's avatar Carlo Joseph Echon 🐟

Changed Announcement URLs and Views for the templates

parent b45e3782
......@@ -2,6 +2,8 @@ from django.urls import path
from . import views
app_name = "announcements"
urlpatterns = [
path('', views.index, name="indexAnnouncements")
path('', views.index, name="indexAnnouncements"),
path('<int:announcement_id>/details', views.details, name="details")
]
\ No newline at end of file
from django.http import HttpResponse
from multiprocessing import context
from django.http import HttpResponse, Http404
from .models import Announcement, Reaction
from django.shortcuts import render
# Create your views here.
def index(request):
display_output = "<u><b>ANNOUNCEMENTS</u></b>: <br>"
announcement_list = Announcement.objects.order_by("pub_date")
context = {
"announcement_list": announcement_list,
}
for object in Announcement.objects.all():
display_output += '''
<b>{title} by {first_name} {last_name}</b> dated {date}:<br>
{body}<br>
Like: {like_tally}<br>
Love: {love_tally}<br>
Angry: {angry_tally}<br>
<br>
'''.format(title = object.announcement_title,
first_name = object.author.first_name,
last_name = object.author.last_name,
date = str(object.pub_date),
body = object.announcement_body,
like_tally = Reaction.objects.filter(announcement_id=object.id).filter(reaction_name="Like").first().tally,
love_tally = Reaction.objects.filter(announcement_id=object.id).filter(reaction_name="Love").first().tally,
angry_tally = Reaction.objects.filter(announcement_id=object.id).filter(reaction_name="Angry").first().tally)
return render(request, "announcements/index.html", context)
return HttpResponse(display_output)
def details(request, announcement_id):
try:
announcement = Announcement.objects.get(pk=announcement_id)
reaction_list = Reaction.objects.order_by("reaction_name")
except Announcement.DoesNotExist:
raise Http404("Announcement does not exist!")
output = {
"announcement": announcement,
"reaction_list": reaction_list,
}
return render(request, "announcements/detail.html", output)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment