I have a view in django that has a filter that uses two dates. I want to calculate the dates in a separate function to tidy it up a bit and return them to the view.
def index(request):
function = month()
objectcount = Entry.objects.filter(entrydate__range=(function[0], function[1])).count() #bleurgh!
context = {
"objectcount": objectcount,
}
return render(request, 'entry/index.html', context)
def month():
today = datetime.datetime.now().date()
first_of_month = "{}-{}-01".format(today.year, today.month)
last = calendar.monthrange(today.year, today.month)[1]
last_of_month = "{}-{}-{}".format(today.year, today.month, last)
return first_of_month, last_of_month
This works fine, but I have to access the variables using function[0] and function[1].
How can I pass these variables so they are accessed from the index view using names - like first_of_month and last_of_month?
return (first_of_month, last_of_month)