Commit 60652b06 authored by Alia Lawraine Olay's avatar Alia Lawraine Olay

Added templates for announcement list and details in views.py

parent 3c614762
...@@ -13,21 +13,25 @@ class Announcement(models.Model): ...@@ -13,21 +13,25 @@ class Announcement(models.Model):
def __str__(self): def __str__(self):
return '"{}" published {}'.format(self.announcement_title, self.pub_date) return '"{}" published {}'.format(self.announcement_title, self.pub_date)
class Reaction(models.Model): class Reaction(models.Model):
announcement = models.ForeignKey( announcement = models.ForeignKey(
Announcement, Announcement,
on_delete=models.CASCADE, on_delete=models.CASCADE,
related_name='reactions' related_name='reactions'
) )
LIKE = 'Like'
LOVE = 'Love'
ANGRY = 'Angry'
REACTION_CHOICES = [ REACTION_CHOICES = [
('Like', 'Like'), (LIKE, 'Like'),
('Love', 'Love'), (LOVE, 'Love'),
('Angry', 'Angry') (ANGRY, 'Angry')
] ]
reaction_name = models.CharField( reaction_name = models.CharField(
max_length=5, max_length=5,
choices=REACTION_CHOICES, choices=REACTION_CHOICES,
default='Like' default=LIKE
) )
tally = models.IntegerField(default=1, editable=False) tally = models.IntegerField(default=1, editable=False)
......
from django.urls import path from django.urls import path
from .views import index from .views import announcement_list_view, announcement_detail_view
urlpatterns = [ urlpatterns = [
path('', index, name='index') # path('', index, name='index')
path('', announcement_list_view, name='announcement-list'),
path('<int:pk>/details', announcement_detail_view, name='announcement-detail'),
] ]
app_name = "announcementboard" app_name = "announcementboard"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Announcement from .models import Announcement
def index(request):
heading = 'ANNOUNCEMENTS:<br>' def announcement_list_view(request):
all_announcements = Announcement.objects.all() context = {}
body = '' context['object_list'] = Announcement.objects.order_by('-pub_date').all()
for announcement in all_announcements: return render(request, 'announcementboard/announcement_list.html', context)
count_like = announcement.reactions.filter(reaction_name='Like').count()
count_love = announcement.reactions.filter(reaction_name='Love').count()
count_angry = announcement.reactions.filter(reaction_name='Angry').count() def announcement_detail_view(request, pk):
body += '{} by {} {} dated {}:<br>{}<br>Like: {}<br>Love: {}<br>Angry: {}<br><br>'.format( announcement = Announcement.objects.get(pk=pk)
announcement.announcement_title, announcement.author.first_name, context = {'announcement': announcement,
announcement.author.last_name, announcement.pub_date, 'count_like': announcement.reactions.filter(reaction_name='Like').count(),
announcement.announcement_body, count_like, 'count_love': announcement.reactions.filter(reaction_name='Love').count(),
count_love, count_angry 'count_angry': announcement.reactions.filter(reaction_name='Angry').count()
) }
return HttpResponse(heading + body) return render(request, 'announcementboard/announcement_detail.html', context)
\ No newline at end of file \ No newline at end of file
...@@ -62,7 +62,7 @@ ROOT_URLCONF = 'widget_group_25.urls' ...@@ -62,7 +62,7 @@ ROOT_URLCONF = 'widget_group_25.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], 'DIRS': [os.path.join(BASE_DIR, 'widget_group_25/templates')],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ 'context_processors': [
...@@ -123,7 +123,8 @@ USE_TZ = True ...@@ -123,7 +123,8 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/ # https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = 'static/' STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'widget_group_25/static')]
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
......
h1 {
color: #393d3f;
font-family: 'Tahoma';
font-size: 35pt;
line-height: 10px;
}
h2 {
color: #546a7b;
font-family: 'Tahoma';
font-size: 20pt;
}
h3 {
color: #546a7b;
font-family: 'Trebuchet MS';
font-size: 16pt;
font-style: italic;
}
body {
color: #393d3f;
font-family: 'Trebuchet MS';
font-size: 12pt;
line-height: 15px;
}
img {
height: 2cm;
}
\ No newline at end of file
h1 {
color: #22223b;
font-family: 'Tahoma';
font-size: 38pt;
}
h3 {
color: #4a4e69;
font-family: 'Trebuchet MS';
font-size: 18pt;
}
body {
color: #4a4e69;
font-family: 'Trebuchet MS';
font-size: 12pt;
}
a:visited {
color: #723d46;
}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}{{ announcement.announcement_title }}{% endblock %}
{% block styles %}
<link rel="stylesheet" href="{% static 'announcementboard/css/detail_styles.css' %}">
{% endblock %}
{% block content %}
{% if announcement.id == 1 or announcement.id == 4 %}
<img src="{% static 'announcementboard/css/quiz.png' %}" alt="quiz">
{% endif %}
{% if announcement.id == 2 %}
<img src="{% static 'announcementboard/css/laptop.png' %}" alt="synch">
{% endif %}
{% if announcement.id == 3 %}
<img src="{% static 'announcementboard/css/project.png' %}" alt="project">
{% endif %}
<h1>{{ announcement.announcement_title }}</h1>
<h2>
by {{ announcement.author.first_name }} {{ announcement.author.last_name }}, {{ announcement.pub_date|date:'d/m/Y' }}
</h2>
{{ announcement.announcement_body }}
<h3>Reactions:</h3>
Like: {{ count_like }}<br>
Love: {{ count_love }}<br>
Angry: {{ count_angry }}
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Announcement Board{% endblock %}
{% block styles %}
<link rel="stylesheet" href="{% static 'announcementboard/css/list_styles.css' %}">
{% endblock %}
{% block content %}
<h1>Announcement Board</h1>
<h3>Important announcements:</h3>
{% for object in object_list %}
<li>
<a href="{% url 'announcementboard:announcement-detail' pk=object.pk %}">
{{ object.announcement_title }} by {{ object.author.first_name }} {{ object.author.last_name }} dated {{ object.pub_date|date:"d/m/Y" }}
</a>
</li>
{% endfor %}
{% endblock %}
<html lang="en">
<head>
<link rel="stylesheet" href="styles.css">
<title>{% block title %}{% endblock %}</title>
{% block styles %}{% endblock %}
</head>
<body>
<div id="content">
{% block content %}{% endblock %}
</div>
{% block scripts %}{% endblock %}
</body>
</html>
\ 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