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 ...@@ -2,6 +2,13 @@ from django.urls import path
from . import views from . import views
app_name = "announcements"
urlpatterns = [ 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 from .models import Announcement, Reaction
# Create your views here. # Create your views here.
def index(request): def index(request):
announcements_list = Announcement.objects.order_by("pub_date")
content = 'ANNOUNCEMENTS:<br/>' context = {
"announcements_list": announcements_list
announcements = Announcement.objects.all() }
reacts = Reaction.objects.all() return render(request, "announcements/index.html", context)
for i in announcements: # announcements_list = Announcement.objects.order_by("pub_date")
announcement_date = i.pub_date.date().strftime("%m/%d/%Y") # output = ", ".join([a.announcement_title for a in announcements_list])
content += f'{i.announcement_title} by {i.author.first_name} {i.author.last_name} dated {announcement_date}:<br/>{i.announcement_body}<br/>' # return HttpResponse(output)
for j in reacts:
if j.announcement == i: # content = 'ANNOUNCEMENTS:<br/>'
content += f'{j.reaction_name}: {j.tally}<br/>' # announcements = Announcement.objects.all()
content += '<br/>' # reacts = Reaction.objects.all()
return HttpResponse(content) # 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/>'
# Display all available Announcements and their respective Reactions in the format # for j in reacts:
# <announcement_title> by <author’s first_name> <author’s last_name> dated <pub_date>: # if j.announcement == i:
# announcement_body # content += f'{j.reaction_name}: {j.tally}<br/>'
# Like: <count> # content += '<br/>'
# Love: <count>
# Angry: <count> # return HttpResponse(content)
# with the heading “ANNOUNCEMENTS: ”. An empty line separates the announcements
# from each other. def detail(request, announcement_id):
try:
# ■ Example: announcement = Announcement.objects.get(pk=announcement_id)
# ANNOUNCEMENTS: except Announcement.DoesNotExist:
# No Synch Session by Alice Wonderland dated 03/16/2022: raise Http404("Announcement does not exist!")
# There will be no synch session next week. return render(request, "announcements/detail.html", {"announcement": announcement})
# 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 # def reactions(request, announcement_id):
\ No newline at end of file # 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