Commit 12c7835a authored by Carlo Joseph Echon's avatar Carlo Joseph Echon 🐟

Added new view called addAnnouncement, and setup new page called add.html

parent 2bcd33fa
from pickle import TRUE
from django.db import models from django.db import models
from homepage.models import WidgetUser from homepage.models import WidgetUser
from django.urls import reverse
# Create your models here. # Create your models here.
class Announcement(models.Model): class Announcement(models.Model):
announcement_title = models.CharField(max_length=50) announcement_title = models.CharField(max_length=50)
announcement_body = models.TextField(max_length=1500) announcement_body = models.TextField(max_length=1500)
pub_date = models.DateTimeField("date published") pub_date = models.DateTimeField("date published", auto_now_add=True)
pub_date.editable = True
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
announcement_image = models.ImageField(null = True, blank = True, upload_to = "announcements/") announcement_image = models.ImageField(null = True, blank = True, upload_to = "announcements/")
def __str__(self): def __str__(self):
return self.announcement_title return self.announcement_title
def get_absolute_url(self):
return reverse('announcements:indexAnnouncements')
class Reaction(models.Model): class Reaction(models.Model):
LIKE = "Like" LIKE = "Like"
LOVE = "Love" LOVE = "Love"
......
{% extends 'base.html' %}
{% load static %}
{% block styles %}
<link rel="stylesheet" type="text/css" href="{% static 'announcements/style.css' %}">
{% endblock %}
{% block title %}New Announcement{% endblock %}
{% block content %}
<h1>New Announcement</h1>
<div>
<form method="POST", enctype="multipart/form-data" >
{% csrf_token %}
{{form.as_p}}
<button>Save Announcement</button>
</div>
{% endblock %}
\ No newline at end of file
...@@ -15,13 +15,15 @@ ...@@ -15,13 +15,15 @@
{% if announcement.announcement_image %} {% if announcement.announcement_image %}
<img src = "{{ announcement.announcement_image.url }}"><br> <img src = "{{ announcement.announcement_image.url }}"><br>
{% endif %} {% endif %}
Reaction:<br> Reactions:<br>
{% for reaction in reaction_list %} {% for reaction in reaction_list %}
{% if reaction.announcement.id == announcement.id %} {% if reaction.announcement.id == announcement.id %}
{{reaction.reaction_name}}: {{reaction.tally}}<br> {{reaction.reaction_name}}: {{reaction.tally}}<br>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</p> </p>
</div> </div>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -13,11 +13,14 @@ ...@@ -13,11 +13,14 @@
{% if announcement_list %} {% if announcement_list %}
<ul> <ul>
{% for announcement in announcement_list %} {% for announcement in announcement_list %}
<li><a href="{% url 'announcements:details' announcement.id %}">{{announcement.announcement_title}}</a> by {{announcement.author.first_name}} {{announcement.author.last_name}} dated {{announcement.pub_date|date:"d/m/Y"}}</li> <li><a href="{% url 'announcements:detailsAnnouncements' announcement.id %}">{{announcement.announcement_title}}</a> by {{announcement.author.first_name}} {{announcement.author.last_name}} dated {{announcement.pub_date|date:"d/m/Y"}}</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% else %} {% else %}
<p>There are no announcements.</p> <p>There are no announcements.</p>
{% endif %} {% endif %}
<a href="{% url 'announcements:addAnnouncements' %}">
<button>New Announcement</button>
</a>
</div> </div>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from . import views from . import views
from .views import addAnnouncement
app_name = "announcements" app_name = "announcements"
urlpatterns = [ urlpatterns = [
path('', views.index, name="indexAnnouncements"), path('', views.index, name="indexAnnouncements"),
path('<int:announcement_id>/details', views.details, name="details") path('<int:announcement_id>/details', views.details, name="detailsAnnouncements"),
path('add/', addAnnouncement.as_view(), name="addAnnouncements"),
] ]
\ No newline at end of file
...@@ -2,6 +2,7 @@ from multiprocessing import context ...@@ -2,6 +2,7 @@ from multiprocessing import context
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from .models import Announcement, Reaction from .models import Announcement, Reaction
from django.shortcuts import render from django.shortcuts import render
from django.views.generic import CreateView
# Create your views here. # Create your views here.
def index(request): def index(request):
...@@ -23,3 +24,9 @@ def details(request, announcement_id): ...@@ -23,3 +24,9 @@ def details(request, announcement_id):
"reaction_list": reaction_list, "reaction_list": reaction_list,
} }
return render(request, "announcements/detail.html", output) return render(request, "announcements/detail.html", output)
class addAnnouncement(CreateView):
model = Announcement
template_name = "announcements/add.html"
fields = ['announcement_title', 'announcement_body', 'author', 'announcement_image']
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