Commit 09d5779c authored by Jan Ericsson Ong Ang's avatar Jan Ericsson Ong Ang

simplified views.py cbv and fbc implementation

parent 6dcb5fd1
from django.shortcuts import render, redirect
from django.views.generic import DetailView, CreateView, UpdateView
from .models import Event, Location
from .forms import AddEventForm
# calendar view for FBV implmentation
def calendar_view(request):
events = Event.objects.all().order_by('id')
events = Event.objects.all()
return render(request, 'calendar.html', {'events': events})
#calendar Detail View for CBV Implementation
class EventDetailView(DetailView):
model = Event, Location
model = Event
template_name = 'event-details.html'
context_object_name = 'event'
class EventAddListView(CreateView):
model = Event, Location
form_class = AddEventForm
model = Event
template_name = "event-add.html"
def post(self, request):
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})
fields = "__all__"
# calendar for editing CBV implementation.
class EventEditView(UpdateView):
model = Event, Location
form_class = AddEventForm
model = Event
template_name = 'event-edit.html'
formatDate = Event.target_datetime.strftime("%d-%b-%y")
# 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)
fields = "__all__"
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