Django JsonResponse returning 'Year and month parameters are required' despite parameters being present in request.GET
I am using 'fullcalendar' and I want to send the month and year to django view and fetch the total hours for each day in that month from this api: 'api/get_calendar_data/' to display the calendar properly. But then I am receiving an unexpected respond where it says it needs the parameters while there are present in the request.
I will provide the code to and you will get what I am doing.
dashboard.html:
<div id="calendar"></div>
<script src="{% static 'js/calendar.js' %}"></script>
calendar.js:
function updateCalendar(month, year) {
console.log(year);
console.log(month);
$.ajax({
url: "/api/get_calendar_data/", // to my django view to fetch data
method: "GET",
data: { year: year, month: month }, // to django view to send month and year
success: function (data) {
// data = { '2024-02-01': 5, '2024-02-02': 7, ..}
for (let date in data) {
let hoursWorked = parseFloat(data[date]);
let calendarDate = new Date(date);
let formattedDate = calendarDate.toISOString().slice(0, 10); //to get the day number 2024-04-23 -> 23
let dayElement = calendarEl.querySelector(
`[data-date="${formattedDate}"]`
);
if (hoursWorked >= 7) {
dayElement.style.backgroundColor = "#5cb85c"; //finished work
} else {
dayElement.style.backgroundColor = "#d9534f"; //not inished
}
}
},
error: function (xhr, status, error) {
console.error("Error fetching calendar data:", error);
},
});
}
//initialize the fullcalendar
document.addEventListener("DOMContentLoaded", function () {
let calendarEl = document.getElementById("calendar");
let calendar = new FullCalendar.Calendar(calendarEl, {});
calendar.render();
let currentDate = calendar.getDate();
let currentMonth = currentDate.getMonth() + 1;
let currentYear = currentDate.getFullYear();
updateCalendar(currentMonth, currentYear); //passing month and year
});
views.py:
class CalendarDataView(View):
def get_firstday_lastday_specific_month(self, year, month):
first_day_current_month = date(year, month, 1)
last_day_current_month = date(year, month, calendar.monthrange(year, month)[1])
return first_day_current_month, last_day_current_month
def get(self, request):
print("request.GET: " ,request.GET)
if 'year' in request.GET and 'month' in request.GET:
try:
year = int(request.GET.get("year"))
month = int(request.GET.get("month"))
print("year + month = ", year + month)
start_date, end_date = self.get_firstday_lastday_specific_month(year, month)
all_month_days = {str(start_date + timedelta(days=i)): 0 for i in range((end_date - start_date).days + 1)}
data = ActivityLogs.objects.filter(user=request.user, date__range=[start_date, end_date]) \
.values('date') \
.annotate(total_hours=Sum('hours_worked')) \
.order_by("date")
for item in data:
all_month_days[str(item['date'])] = item['total_hours']
return JsonResponse(all_month_days)
except Exception as e:
return JsonResponse({'error': str(e)}, status=500)
else:
return JsonResponse({'error': 'Year and month parameters are required'}, status=400)
the url:
path('api/get_calendar_data/', views.CalendarDataView.as_view(), name='get_calendar_data'),
and the only respond am getting is:
{
"error": "Year and month parameters are required"
}
even though I the request in the print in the view are printing along with "year+month":
request.GET: <QueryDict: {'month': ['2'], 'year': ['2024']}>
2026
I just don't get why wouldn't return the dict with the prober data?