Commit b67dc1ad authored by Maso Crisostomo's avatar Maso Crisostomo

Updated announcements app with templates

parent a93908a9
h1 {
color: white;
font-weight: bold;
}
p {
color: white;
font-weight: normal;
}
body {
background-color: black;
}
a {
color: orangered;
}
\ No newline at end of file
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="{% static 'announcements/style.css' %}">
<title>My Blog Site</title>
</head>
<body>
<!-- <script src="index.js"></script> -->
{% block content %}
{% endblock %}
</body>
</html>
\ No newline at end of file
{% extends "announcements/base.html" %}
{% block content %}
<h1> {{ announcement.announcement_title }} </h1>
<p> {{ announcement.announcement_body }} </p>
{% endblock %}
\ No newline at end of file
{% extends "announcements/base.html" %}
{% block content %}
{% if announcements_list %}
<ul>
{% for announcement in announcements_list %}
<li><a href="{% url 'announcements:detail' announcement.id %}">{{ announcement.announcement_title }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No announcements are available.</p>
{% endif %}
{% endblock %}
......@@ -2,6 +2,13 @@ from django.urls import path
from . import views
app_name = "announcements"
urlpatterns = [
path('', views.index, name='index')
path('', views.index, name='index'),
#announcements/1
path("<int:announcement_id>", views.detail, name="detail"),
#announcements/1/reactions
path("<int:announcement_id>/reactions", views.reactions, name="reactions"),
#announcements/1/react
path("<int:announcement_id>/react", views.react, name="react")
]
\ No newline at end of file
from django.http import HttpResponse
from django.http import HttpResponse, Http404
from django.shortcuts import render
from .models import Announcement, Reaction
# Create your views here.
def index(request):
content = 'ANNOUNCEMENTS:<br/>'
announcements = Announcement.objects.all()
reacts = Reaction.objects.all()
for i in announcements:
announcement_date = i.pub_date.date().strftime("%m/%d/%Y")
content += f'{i.announcement_title} by {i.author.first_name} {i.author.last_name} dated {announcement_date}:<br/>{i.announcement_body}<br/>'
for j in reacts:
if j.announcement == i:
content += f'{j.reaction_name}: {j.tally}<br/>'
content += '<br/>'
return HttpResponse(content)
# Display all available Announcements and their respective Reactions in the format
# <announcement_title> by <author’s first_name> <author’s last_name> dated <pub_date>:
# announcement_body
# Like: <count>
# Love: <count>
# Angry: <count>
# with the heading “ANNOUNCEMENTS: ”. An empty line separates the announcements
# from each other.
# ■ Example:
# ANNOUNCEMENTS:
# No Synch Session by Alice Wonderland dated 03/16/2022:
# There will be no synch session next week.
# Like: 3
# Love: 5
# Angry: 0
# Project Deadline by Little Luly dated 03/16/2022
# Deadline for the project is next month.
# Like: 4
# Love: 6
# Angry: 3
\ No newline at end of file
announcements_list = Announcement.objects.order_by("pub_date")
context = {
"announcements_list": announcements_list
}
return render(request, "announcements/index.html", context)
# announcements_list = Announcement.objects.order_by("pub_date")
# output = ", ".join([a.announcement_title for a in announcements_list])
# return HttpResponse(output)
# content = 'ANNOUNCEMENTS:<br/>'
# announcements = Announcement.objects.all()
# reacts = Reaction.objects.all()
# for i in announcements:
# announcement_date = i.pub_date.date().strftime("%m/%d/%Y")
# content += f'{i.announcement_title} by {i.author.first_name} {i.author.last_name} dated {announcement_date}:<br/>{i.announcement_body}<br/>'
# for j in reacts:
# if j.announcement == i:
# content += f'{j.reaction_name}: {j.tally}<br/>'
# content += '<br/>'
# return HttpResponse(content)
def detail(request, announcement_id):
try:
announcement = Announcement.objects.get(pk=announcement_id)
except Announcement.DoesNotExist:
raise Http404("Announcement does not exist!")
return render(request, "announcements/detail.html", {"announcement": announcement})
# def reactions(request, announcement_id):
# response = "This are the reactions to announcement # %s."
# return HttpResponse(response % announcement_id)
# def react(request, announcement_id):
# return HttpResponse("You are reacting to announcement # %s." % announcement_id)
\ No newline at end of file
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