I have next code and trying to add to context value the combine event date and time
in models.py
class Event(models.Model):
event_date= models.DateField(auto_now=False, auto_now_add=False)
evet_time= models.TimeField(auto_now=False, auto_now_add=False)
in view.py
class EventList(FilterMixin, ListView):
model = Event
filter_class = EventFilter
template_name = 'events/event_list.html'
def get_context_data(self, *args, **kwargs):
context = super(EventList, self).get_context_data(*args, **kwargs)
context["filter_form"] = EventFilter(data=self.request.GET or None)
return context
need to add next values to context.
context["now"] = datetime.utcnow() + timedelta(hours=1)
- work fine
context["event_date_time"]
- how to combine date and time from db table?
Trying next:
context["event_date_time"] = datetime.combine(self.event_date, self.event_time)
Don't work.
Exception Value:
'EventList' object has no attribute 'event_date'
Thx for help.
You are calling self.event_date and self.event_time where 'self' is the EventList view, and not the Event model where those fields actually are.
As you are using a list view, where the contents of the view are based on a queryset, rather than one particular instance of a model, I would create model method on Event to handle this.
class Event(models.Model):
event_date= models.DateField(auto_now=False, auto_now_add=False)
evet_time= models.TimeField(auto_now=False, auto_now_add=False)
def event_date_time(self):
return datetime.combine(self.event_date, self.event_time)
You don't need to add it to the context as it will be available on every instance that you iterate through in the template, something like this:
{% for event in event_list %}
....html here...
{{ event.event_date_time }}
....
{% endfor %}