Added addAnnouncement for announcement app and other requirements for Final project

parent 7843baa5
from django import forms
from .models import Announcement, Reaction
class AddAnnouncementForm(forms.ModelForm):
class Meta:
model = Announcement
fields = '__all__'
{% extends "announcements/base.html" %}
{% load static %}
{% block styles %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
{% endblock %}
{% block content %}
<h1>New Announcement</h1>
<form action="{% url 'announcements:addAnnouncement' %}" method="POST">
{% csrf_token %}
{{ add_announcement }}
<p><button class="button" type="submit">Save Announcement</button></p>
</form>
<br>
<p1>
<a href = "/announcements/">Back to Announcement Page</a>
</p1>
{% endblock %}
\ No newline at end of file
...@@ -22,6 +22,10 @@ ...@@ -22,6 +22,10 @@
</p> </p>
{% endfor %} {% endfor %}
</ol> </ol>
<form action="/announcements/add">
<input type="submit" value="New Announcement" />
</form>
</body> </body>
{% endblock %} {% endblock %}
from django.urls import path from django.urls import path
from .views import announcements, details from .views import AnnouncementView, details, addAnnouncement
urlpatterns = [ urlpatterns = [
path('', announcements, name='announcements'), #path('', announcements, name='announcements'),
path('announcements/<int:announcement_id>/details/', details, name='details') path('', AnnouncementView.as_view(), name='announcement'),
path('<int:announcement_id>/details/', details, name='details'),
path('add/', addAnnouncement, name='addAnnouncement')
] ]
app_name = "announcements" app_name = "announcements"
from django.shortcuts import render from django.shortcuts import render, redirect
from django.http import HttpResponse from django.http import HttpResponse
from . import models from . import models
from .models import Announcement, Reaction from .models import Announcement, Reaction
from .forms import AddAnnouncementForm
from django.views import View
from django.views.generic.list import ListView
def index(request): class AnnouncementView(View):
return HttpResponse('This is the Announcement Board!') def get(self, request):
all_announcements = models.Announcement.objects.order_by("-id")
def announcements(request): context = {
all_announcements = models.Announcement.objects.order_by("-id") "all_announcements": all_announcements
context = { }
"all_announcements": all_announcements return render(request, 'announcements/announcements.html', context)
}
return render(request, 'announcements/announcements.html', context)
def details(request, announcement_id): def details(request, announcement_id):
announcement_details = models.Announcement.objects.get(pk=announcement_id) announcement_details = models.Announcement.objects.get(pk=announcement_id)
...@@ -33,3 +34,13 @@ def details(request, announcement_id): ...@@ -33,3 +34,13 @@ def details(request, announcement_id):
"angry": angry, "angry": angry,
} }
return render(request, "announcements/details.html", context) return render(request, "announcements/details.html", context)
def addAnnouncement(request):
if request.method == 'POST':
add_announcement = AddAnnouncementForm(request.POST)
if add_announcement.is_valid():
new_announcement = add_announcement.save()
return redirect('announcements:addAnnouncement')
else:
add_announcement = AddAnnouncementForm()
return render(request, 'announcements/addAnnouncements.html', {'add_announcement': add_announcement})
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