Created app templates

parent 3505d8fc
from django import forms
from .models import Announcement
class AnnouncementForm(forms.ModelForm):
class Meta:
model = Announcement
fields = ['title', 'body', 'author']
\ No newline at end of file
# Generated by Django 4.1.7 on 2023-05-10 05:08
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('announcement_board', '0006_alter_reaction_announcement'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_datetime',
field=models.DateTimeField(default=django.utils.timezone.now),
),
]
from django.db import models from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.utils import timezone
from dashboard import models as dashboard_models from dashboard import models as dashboard_models
class Announcement(models.Model): class Announcement(models.Model):
title = models.CharField(max_length=255, default="") title = models.CharField(max_length=255, default="")
body = models.TextField(default="") body = models.TextField(default="")
author = models.ForeignKey(dashboard_models.WidgetUser, on_delete=models.CASCADE) author = models.ForeignKey(dashboard_models.WidgetUser, on_delete=models.CASCADE)
pub_datetime = models.DateTimeField() pub_datetime = models.DateTimeField(default=timezone.now)
def __str__(self): def __str__(self):
return '{} by {} {} published on {}: {}'.format( return '{} by {} {} published on {}, {}: {}'.format(
self.title, self.title,
self.author.first_name, self.author.first_name,
self.author.last_name, self.author.last_name,
self.pub_datetime.strftime("%m/%d/%Y %I:%M %p"), self.pub_datetime.strftime("%m/%d/%y"),
self.pub_datetime.strftime("%I:%M %p"),
self.body self.body
) )
def formatted_date(self):
return '{}'.format(self.pub_datetime.strftime("%m/%d/%y"))
def formatted_time(self):
return '{}'.format(self.pub_datetime.strftime("%I:%M %p"))
def getLikeReactions(self):
try:
return 'Like: {}'.format(Reaction.objects.get(name="Like", announcement=self).tally)
except:
return 'Like: 0'
def getLoveReactions(self):
try:
return 'Love: {}'.format(Reaction.objects.get(name="Love", announcement=self).tally)
except:
return 'Love: 0'
def getAngryReactions(self):
try:
return 'Angry: {}'.format(Reaction.objects.get(name="Angry", announcement=self).tally)
except:
return 'Angry: 0'
def get_absolute_url(self):
return reverse('announcement_board:announcement-details', kwargs={'pk': self.pk})
class Reaction(models.Model): class Reaction(models.Model):
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, related_name='reactions') announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, related_name='reactions')
......
{% extends 'base.html' %}
{% load static %}
{% block title %}Add Announcement{% endblock %}
{% block content %}
<h2>Add a new announcement:</h2>
<br>
<form method="post">
{% csrf_token %}
{% for field in form %}
{{field.label}}:{{field}} <br><br>
{% endfor %}
<input type="submit" value="Add Announcement">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}{{ object.title }}{% endblock %}
{% block content %}
<h1>{{ object.title }}</h1>
<h2>by {{ object.author.first_name }} {{ object.author.last_name }}</h2>
<h2>{{ object.formatted_date }}, {{ object.formatted_time }}</h2>
<p>
{{ object.body }}
<br><br>
{{ object.getLikeReactions }}
<br>
{{ object.getLoveReactions }}
<br>
{{ object.getAngryReactions }}
<br><br>
<button class="button"><a href="/announcements/{{ object.pk }}/edit">Edit Announcement</a></button>
</p>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Announcement{% endblock %}
{% block content %}
<h2>Edit announcement:</h2>
<br>
<form method="post">
{% csrf_token %}
{% for field in form %}
{{field.label}}:{{field}} <br><br>
{% endfor %}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Widget's Announcement Board{% endblock %}
{% block content %}
<h1>Welcome to Widget's Announcement Board!</h1>
<h2>Announcements:</h2>
<p>
{% for object in announcements %}
<li>
<a href="{{ object.get_absolute_url }}">{{ object.title }} by {{ object.author.first_name }} {{ object.author.last_name }}</a>
</li>
{% endfor %}
<br>
<button class="button"><a href="/announcements/add">New Announcement</a></button>
</p>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index from .views import announcements, AnnouncementDetailView, AnnouncementCreateView, AnnouncementUpdateView
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('', announcements, name='announcements'),
path('<int:pk>/details/', AnnouncementDetailView.as_view(), name='announcement-details'),
path('add/', AnnouncementCreateView.as_view(), name='announcement-add'),
path('<int:pk>/edit/', AnnouncementUpdateView.as_view(), name='announcement-edit'),
] ]
app_name = "announcement_board" app_name = "announcement_board"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render, redirect
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django.http import HttpResponse from django.http import HttpResponse
from .models import Announcement
from .models import Reaction
def index(request): from .models import Announcement, Reaction
from .forms import AnnouncementForm
def announcements(request):
'''
return_string = "<p>Widget's Announcement Board</p> <p>Announcements:<br>" return_string = "<p>Widget's Announcement Board</p> <p>Announcements:<br>"
for announcements in Announcement.objects.all(): for announcements in Announcement.objects.all():
...@@ -31,7 +35,11 @@ def index(request): ...@@ -31,7 +35,11 @@ def index(request):
html_string = '<html><body>{}</body></html>'.format(return_string) html_string = '<html><body>{}</body></html>'.format(return_string)
return HttpResponse(return_string) return HttpResponse(return_string)
'''
return render(request, 'announcement_board/announcements.html', {'announcements':Announcement.objects.order_by('-pub_datetime')})
'''
# Sorting the reactions to Like, Love, and Angry order. # Sorting the reactions to Like, Love, and Angry order.
def sortReactions(list): def sortReactions(list):
sortedReactionList = ['']*3 sortedReactionList = ['']*3
...@@ -45,3 +53,30 @@ def sortReactions(list): ...@@ -45,3 +53,30 @@ def sortReactions(list):
sortedReactionList[2] = reaction sortedReactionList[2] = reaction
return sortedReactionList return sortedReactionList
'''
class AnnouncementDetailView(DetailView):
model = Announcement
template_name = 'announcement_board/announcement-details.html'
class AnnouncementCreateView(CreateView):
model = Announcement
fields = 'title', 'body', 'author'
template_name = 'announcement_board/announcement-add.html'
class AnnouncementUpdateView(UpdateView):
model = Announcement
fields = 'title', 'body', 'author'
template_name = 'announcement_board/announcement-edit.html'
def announcement_view(request):
if request.method == 'POST':
form = AnnouncementForm(request.POST)
if form.is_valid():
new_announcement = form.save()
return redirect('announcement-detail', pk=new_announcement.pk)
else:
form = AnnouncementForm()
return render(request, 'announcement_board/announcement-detail.html', {'form': form})
\ No newline at end of file
...@@ -64,7 +64,7 @@ ROOT_URLCONF = 'widget_jenicaesports.urls' ...@@ -64,7 +64,7 @@ ROOT_URLCONF = 'widget_jenicaesports.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], 'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ 'context_processors': [
......
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