Commit 411e7579 authored by Joshua Son's avatar Joshua Son

Update: Ability to make announcements and a back button in said creation page

parent dcfbdfaa
from django.forms import ModelForm
from .models import Announcement
class AnnouncementForm(ModelForm):
class Meta:
model = Announcement
fields = ["announcement_title", "announcement_body", "author", "pub_date"]
\ No newline at end of file
# Generated by Django 3.2.12 on 2022-05-26 11:44
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0017_announcement_announce_image'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='announce_image',
field=models.ImageField(blank=True, default='faceless.png', null=True, upload_to=''),
),
]
...@@ -8,7 +8,7 @@ class Announcement(models.Model): ...@@ -8,7 +8,7 @@ class Announcement(models.Model):
announcement_body = models.CharField(max_length=500) announcement_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("date published") pub_date = models.DateTimeField("date published")
reaction_list = models.ForeignKey('Reaction', on_delete=models.CASCADE, related_name='+', default=1) reaction_list = models.ForeignKey('Reaction', on_delete=models.CASCADE, related_name='+', default=1)
announce_image = models.ImageField(default="faceless_9ltCukV.png", null=True, blank=True) announce_image = models.ImageField(default="faceless.png", null=True, blank=True)
def __str__(self): def __str__(self):
return self.announcement_title return self.announcement_title
......
...@@ -11,4 +11,10 @@ ...@@ -11,4 +11,10 @@
{% else %} {% else %}
<p> No announcements here! </p> <p> No announcements here! </p>
{% endif %} {% endif %}
<form action="add">
<input type="Submit" value="Make an announcement!">
</form>
{% endblock %} {% endblock %}
\ No newline at end of file
{% extends "announcement/base.html" %}
{% block content %}
<h1> NEW ANNOUNCEMENT </h1>
<form action="{% url 'add' %}" method="POST">
{% csrf_token %}
<h3> Title: </h3> <input type="text" name="Title">
<h3> Body: </h3> <textarea rows="5" cols="60" name="Body" placeholder="Type your announcement here..."> </textarea>
<br>
<br>
<select class="custom-select mb-4" name="Author">
<option selected> Choose an author </option>
{% for author in Author %}
<option value="{{ author.pk }}"> {{ author.first_name }} {{ author.last_name }} </option>
{% endfor %}
</select>
<br>
<br>
<button class="button" type="submit"> Save Announcement </button>
</form>
<br>
<br>
<a href="{% url 'index' %}"> Go back </a>
{% endblock %}
\ No newline at end of file
{% extends "announcement/base.html" %}
{% block content %}
<form method="POST" action = "{% url 'announcements:newannounce' %}">
{% csrf_token %}
{{ announce_form.as_p }}
<button class="button" type="submit"> Save </button>
</form>
{% endblock %}
\ No newline at end of file
...@@ -4,5 +4,7 @@ from . import views ...@@ -4,5 +4,7 @@ from . import views
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path("<int:announcement_id>", views.detail, name="detail"), path("<int:announcement_id>/", views.detail, name="detail"),
path("add/", views.add, name="add"),
#path("add/newAnnounce", views.newAnnounce, name="newAnnounce")
] ]
\ No newline at end of file
from django.http import Http404
from django.shortcuts import render
from .models import Announcement, Reaction
from django.http import Http404, HttpResponse
from django.shortcuts import render, redirect
from .models import Announcement, Reaction
from homepage.models import WidgetUser
import datetime
from .forms import AnnouncementForm
# Create your views here. # Create your views here.
#def show_announcements(request):
#announcement = Announcement.objects.all()
#return render(request, 'announcement/announcement_board.html', {'announcements': announcement})
def index(request): def index(request):
announce_list = Announcement.objects.order_by("pub_date") announce_list = Announcement.objects.order_by("pub_date")
...@@ -16,6 +15,22 @@ def index(request): ...@@ -16,6 +15,22 @@ def index(request):
} }
return render(request, "announcement/announcement_page.html", context) return render(request, "announcement/announcement_page.html", context)
def add(request):
author = WidgetUser.objects.all()
context = {
"Author": author,
}
if request.method == "POST":
title = request.POST.get("Title")
body = request.POST.get("Body")
author = request.POST.get("Author")
pub_date = datetime.datetime.now()
announcement = Announcement.objects.create(author_id = author, announcement_title = title, announcement_body = body, pub_date = pub_date)
return redirect('add')
return render(request, "announcement/create_announcement.html", context)
def detail(request, announcement_id): def detail(request, announcement_id):
try: try:
announce = Announcement.objects.get(pk=announcement_id) announce = Announcement.objects.get(pk=announcement_id)
...@@ -24,3 +39,4 @@ def detail(request, announcement_id): ...@@ -24,3 +39,4 @@ def detail(request, announcement_id):
reacts = Reaction.objects.filter(announcement_id=announce).order_by("reaction_name") reacts = Reaction.objects.filter(announcement_id=announce).order_by("reaction_name")
return render(request, "announcement/announcement_board.html", {"announce": announce, "reacts": reacts}) return render(request, "announcement/announcement_board.html", {"announce": announce, "reacts": reacts})
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