Commit a1fcb106 authored by Jan Ericsson Ong Ang's avatar Jan Ericsson Ong Ang

Updated views.py

parent 511f08de
from django.http import HttpResponse from django.shortcuts import render, redirect
from django.views.generic import DetailView, CreateView, UpdateView
from .models import Event, Location from .models import Event, Location
# calendar view from .models # calendar view for FBV implmentation
def calendarIndex(request): def calendar_view(request):
title = 'Widget’s Calendar of Activities<br><br>' calendars = Event.objects.all().order_by('id')
events = Event.objects.all() return render(request, calendar.html, {'calendars', calendars})
output_view = "" #calendar Detail View for CBV Implementation
for event in events: class EventDetailView(DetailView):
date_and_time = "Date and Time: " + event.target_datetime.strftime("%m/%d/%Y, %I:%M %p") + "<br>" model = Event
event_activity = "Activity: " + event.activity + "<br>" template_name = 'event-details.html'
estimated_duration = "Estimated Hours: " + str(event.estimated_hours) + "<br>" context_object_name = 'event'
course_section = "Course/Section: " + event.course.code + " " + event.course.title + "-" + event.course.section + "<br>"
event_mode = "Mode: " + event.location.mode + "<br>"
event_venue = "Venue: " + event.location.venue + "<br><br>"
output_view = output_view + date_and_time + event_activity + estimated_duration + course_section + event_mode + event_venue class EventAddListView(CreateView):
model = Event
form_class = AddEventForm
template_name = "event-add.html"
return HttpResponse(title + output_view) def post(self, request):
\ No newline at end of file form = AddEventForm(request.POST)
if form.is_valid():
# onSave is called, the form will have essential data and this includes pk as well.
new_event = form.save()
# pass the pk and redirect.
return redirect('event_details', pk = new_event.pk)
else:
return render(request, 'event-details.html', {'form': form})
# calendar for editing CBV implementation.
class EventEditView(UpdateView):
model = Event
form_class = AddEventForm
template_name = 'event-edit.html'
# This will get the object, and this object is used to tell Django
# that we are updating.
def get_object(self, queryset = None):
pk = self.kwargs.get('pk')
return Event.objects.get(pk=pk)
def post(self, request, pk):
event = self.get_object()
form = AddEventForm(request.POST, instance = event)
if form.is_valid():
# onSave is called, the form will have essential data and this includes pk as well.
update_event = form.save()
# pass the pk and redirect.
return redirect('event_details', pk = update_event.pk)
else:
return render(request, 'event-details.html', {'form': form})
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