[Answered ]-Pass additional data in DetailView Django

1👍

You can use self.object to calculate timedelta:

def get_context_data(self, *args, **kwargs):
    context = super(MocDetailView, self).get_context_data(*args, **kwargs)
    time = self.object.initiation_date
    today = datetime.date.today()
    time_delta = today - time
    td=str(time_delta)
    context['td'] = td
    return context

This should work because Django assign the result of get_object() method to self.object variable. You can check the source code here.

Also Django passes object into get_context_data directly, so it’s also possible to get it from kwargs:

def get_context_data(self, *args, **kwargs):
    context = super(MocDetailView, self).get_context_data(*args, **kwargs)
    time = kwargs['object'].initiation_date
    # rest of the code 

Leave a comment