Commit 0f677172 authored by Cherish Magpayo's avatar Cherish Magpayo

Add adding new announcement view and change index view to cbv

parent 1c329606
from django.http import HttpResponse, Http404
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.views import View
from announcements.models import Announcement
from .forms import AnnouncementForm
# Create your views here.
def index(request):
class IndexView(View):
def get(self, request):
announcement_list = Announcement.objects.order_by("-pub_date")
context = {
"announcement_list": announcement_list,
}
return render(request, "announcements/index.html", context)
'''def index(request):
announcement_list = Announcement.objects.order_by("pub_date")
announcement_list = Announcement.objects.order_by("-pub_date")
context = {
"announcement_list": announcement_list,
}
return render(request, "announcements/index.html", context)
return render(request, "announcements/index.html", context) '''
def details(request, announcement_id):
try:
......@@ -20,6 +30,15 @@ def details(request, announcement_id):
raise Http404("Announcement does not exist!")
return render(request, "announcements/details.html", {"announcement": announcement})
def add(request):
if request.method == "POST":
announcement_form = AnnouncementForm(request.POST, request.FILES)
if announcement_form.is_valid():
announcement_form.save()
return redirect("announcements:add")
else:
announcement_form = AnnouncementForm()
return render(request, "announcements/add.html", {"announcement_form": announcement_form})
""" midterms
......
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