added forms to announcements to allow new announcements to be created

parent 248943fc
from django.forms import ModelForm
from .models import Announcement
class AnnouncementForm(ModelForm):
class Meta:
model = Announcement
fields = ["announcement_title", "announcement_body", "author", "images"]
\ No newline at end of file
# Generated by Django 4.0.3 on 2022-05-22 20:35
import datetime
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0007_announcement_images'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateField(default=datetime.datetime.now, verbose_name='Date Published'),
),
]
from datetime import datetime
from multiprocessing import allow_connection_pickling
from django.db import models
from homepage.models import WidgetUser
......@@ -8,10 +9,10 @@ class Announcement(models.Model):
announcement_body = models.CharField(max_length=500)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE,default = 1)
images = models.ImageField (upload_to="announcement", default = "default.jpg")
pub_date = models.DateField("Date Published")
pub_date = models.DateField("Date Published", default=datetime.now)
def __str__(self):
return f"{self.announcement_title} {self.announcement_body} {self.pub_date}"
return f"{self.announcement_title} {self.pub_date}"
class Reaction(models.Model):
reaction_name = models.CharField(max_length=10)
......@@ -19,4 +20,4 @@ class Reaction(models.Model):
announcement = models.ForeignKey(Announcement,on_delete=models.CASCADE, default = 1)
def __str__(self):
return f"{self.reaction_name} {self.tally}"
\ No newline at end of file
return f"{self.reaction_name}"
\ No newline at end of file
{% extends "announcements/base.html" %}
{% block title %}New Announcement{% endblock %}
{% block content %}
<h1>New Announcement</h1>
<form method="POST" action="{% url 'announcement:add' %}"
enctype="multipart/form-data">
{% csrf_token %}
{{ announcementForm.as_p }}
<button class="button" type="submit">Save Announcement</button>
<button onclick="window.location.href='../';">Back to Announcement Board</button>
</form>
{% endblock %}
\ No newline at end of file
......@@ -12,4 +12,5 @@
<li>{{ reaction.reaction_name }}: {{reaction.tally}}</li>
{% endfor %}
</ul></p>
<button onclick="window.location.href='../../';">Back to Announcement Board</button>
{% endblock %}
\ No newline at end of file
......@@ -12,6 +12,7 @@
{% endfor %}
</ul>
{% else %}
<p>No users are available</p>
<p>No announcements are available</p>
{% endif %}
<button onclick="window.location.href='add/';">New Announcement</button>
{% endblock %}
\ No newline at end of file
......@@ -3,9 +3,11 @@ from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='announcementboardIndex'),
path('', views.index, name="index"),
# announcement/1/details
path("<int:announcement_id>/details/", views.details, name="details")
path("<int:announcement_id>/details/", views.details, name="details"),
# announcement/add/
path("add/", views.add, name="add")
]
app_name = "announcement"
\ No newline at end of file
from django.http import HttpResponse, Http404
from django.shortcuts import render
from django.shortcuts import render, redirect
from .forms import AnnouncementForm
from .models import Announcement, Reaction
announcementList = Announcement.objects.order_by("pub_date")
reactionList = Reaction.objects.all()
# Create your views here.
def index(request):
announcementList = Announcement.objects.order_by("pub_date")
reactionList = Reaction.objects.all()
context = {
"announcementList": announcementList,
}
......@@ -14,6 +16,9 @@ def index(request):
def details(request, announcement_id):
announcementList = Announcement.objects.order_by("pub_date")
reactionList = Reaction.objects.all()
try:
announcement = Announcement.objects.get(pk=announcement_id)
except Announcement.DoesNotExist:
......@@ -27,3 +32,14 @@ def details(request, announcement_id):
"reactionList": reactionList,
}
return render(request, "announcements/details.html", context)
def add(request):
if request.method == "POST":
announcementForm = AnnouncementForm(request.POST)
if announcementForm.is_valid():
new_announcement = announcementForm.save()
return redirect("announcement:index")
else:
announcementForm = AnnouncementForm()
return render(request, "announcements/add.html",
{"announcementForm":AnnouncementForm})
\ 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