Yes, I did have GetPatientsForClinic as a method of CGUDefault. That makes perfect OOP sense to me; the CGUDefault class (the controller) services the template (the view) and with the method being used to extend the functionality of the UI, it should belong to the servicing controller. In other languages (.NET, Delphi etc) it’s perfectly normal for a controller to have multiple get and post methods. Is Django limited in this way? I thought the whole idea of MVC routing was to avoid the limitations of HTTP is a fairly simple and coherent way.
Anyway, I tried making GetPatientsForClinic a standard view function, which is what you see below, but still no joy:
from datetime import datetime, timedelta
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render
from django.contrib.auth import logout
from django.views.generic import TemplateView
from . import helper_classes
from .models import MasterActivityTable, STAFF
from django.core import serializers
from django.http import JsonResponse
class CGUDefault(TemplateView):
template_name = 'PollsterSystem/CGUDefault.html'
def get(self, request):
try:
if not request.user.is_authenticated:
#TODO Add authorisation check
context = { "errorMessage": "You must log in before you can access any system pages"}
return render(request, 'PollsterSystem/CGULogin.html', context)
else:
#This GET has two primary states. The initial state is when the system first opens the Default.html
#page, where there are no search criteria in the request body. The main state is when the user
#submits a search and the request body does have the search criteria.
testSessionObj = helper_classes.TestSession(None, None, request.session['TestSessionObject']) # Pull the dictionary out of session
_text_value = testSessionObj.text_value
_number_value = testSessionObj.number_value
# Extract the dates from the selection criteria
_dateFrom = None
_dateTo = None
_bookedDate = None
try:
for _key in request.GET:
if _key == "txtCriteriaDateFrom":
_dateFromStr = request.GET[_key]
_dateFrom = datetime.strptime(_dateFromStr, '%Y-%m-%d') #IMPORTANT..The format string is different to a template filter and is specific!
if _key == "txtCriteriaDateTo":
_dateToStr = request.GET[_key]
_dateTo = datetime.strptime(_dateToStr, '%Y-%m-%d')
# if _key == "ddlCriteriaClinic":
# _bookedDateStr = request.GET[_key]
# _bookedDate = datetime.strptime(_bookedDateStr, '%Y-%m-%d')
except Exception as ex:
#The most likely error at this point is conversion of the date strings to datetime objects!
context = {"errorMessage": "CGUDefault.Get (2) : " + str(ex)}
return render(request, self.template_name, context)
#If the criteria are not provided, create the default criteria
if _dateFrom == None:
_dateFrom = datetime.today() - timedelta(days=2)
if _dateTo == None:
_dateTo = datetime.today() + timedelta(days=5)
_staffObj = STAFF.objects.get(EMPLOYEE_NUMBER = request.user.username)
#_clinics = MasterActivityTable.objects.filter(STAFF_CODE_1=_staffObj.STAFF_CODE, BOOKED_DATE__range=(_dateFrom, _dateTo))
_clinics = MasterActivityTable.objects.filter(STAFF_CODE_1=_staffObj.STAFF_CODE, BOOKED_DATE__range=(_dateFrom, _dateTo))
# if _bookedDate == None:
# _patients = None
# else:
# _patients = MasterActivityTable.objects.filter(BOOKED_DATE=_bookedDate)
context = {
"text_value" : _text_value,
"number_value" : _number_value,
"criteriaFor" : _staffObj.NAME,
"criteriaDateFrom" : _dateFrom,
"criteriaDateTo" : _dateTo,
"criteriaClinics" : _clinics,
#"criteriaPatients" : _patients,
}
return render(request, self.template_name, context)
except Exception as ex:
context = {"errorMessage": "CGUDefault.Get (1) : " + str(ex)}
return render(request, self.template_name, context)
def post(self, request):
# At the moment the only post is to log out
logout(request)
return render(request, 'PollsterSystem/CGULogin.html', None)
def GetPatientsForClinic(self, request):
try:
_bookedDateStr = request.GET["BOOKED_DATE"]
_bookedDate = datetime.strptime(_bookedDateStr, '%Y-%m-%d')
_patients = MasterActivityTable.objects.filter(BOOKED_DATE=_bookedDate)
_patientsJSON = serializers.serialize('JSON', _patients)
return JsonResponse(_patientsJSON)
except Exception as ex:
context = {"errorMessage": "CGUDefault.GetPatientsForClinic : " + str(ex)}
return render(request, self.template_name, context)
I tried using fetch as well as jquery and although the messaging is better it still gave me the same result. Even if I hard code the full URI in I don’t get anything.
I can do the cascading drop down lists using the primary “get” method, but want to understand how I can use javascript in this way. I have a project coming up which will require at least 4 separate get type calls and at least two forms on one page.