0

i want to implement an event calendar. i am faced with the problem of displaying the closest event to today's date. to find the nearest date, i use __gte in queryset, after queryset finds all the nearest dates, I want to highlight the first one with a different color here is my solution could you tell me what i'm doing wrong?

This is my Model

class Events(models.Model):
   title = models.CharField(max_length=100)
   slug = models.SlugField()
   start_time = models.DateTimeField()
   end_time = models.DateTimeField()



   def __str__(self):
      return self.title

   @property
   def get_html_url(self):
      url = reverse('cal:events', args=(self.slug,))
      return f'<a href="{url}">'

And my HTMLCalendar

from datetime import datetime, timedelta
from calendar import HTMLCalendar

from .models import Events


class Calendar(HTMLCalendar):

  def __init__(self, year=None, month=datetime.now().month):
        self.year = year
        self.month = month
        super(Calendar, self).__init__()

# formats a day as a td
# filter events by day

def formatday(self, day, events):
    events_per_day = events.filter(start_time__day=day)
    d = ''
    if Events.objects.filter(start_time__day=day, start_time__month=self.month).exists():
        for event in events_per_day:
            d += f'{event.get_html_url}'

        if day != 0:
            ev = Events.objects.filter(start_time__gt=datetime.now()).first()
            if ev:
                return f"<td>{d}<span style='color:red;' class='date'>{day}</span></a></td>"
            else:
                return f"<td>{d}<span style='color:aliceblue;' class='date'>{day}</span></a></td>"
        return '<td></td>'
    else:
        if day != 0:
            return f"<td><b><span class='date'>{day}</span>  </b></td>"
        return '<td></td>'

# formats a week as a tr
def formatweek(self, theweek, events):
    week = ''
    for d, weekday in theweek:
        week += self.formatday(d, events)
    return f'<tr> {week} </tr>'

# formats a month as a table
# filter events by year and month
def formatmonth(self, withyear=True, ):
    events = Events.objects.filter(start_time__year=self.year, start_time__month=self.month)

    cal = f'<table border="0" cellpadding="0" cellspacing="0" class="calendar">\n'
    cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}\n'
    cal += f'{self.formatweekheader()}\n'
    for week in self.monthdays2calendar(self.year, self.month):
        cal += f'{self.formatweek(week, events)}\n'
    return cal

my solution is in the format day () function I am executing a query and if there is the first element I want to highlight it in red and paint over all the others with a different color

1 Answers1

0

I'd consider moving the formatting to your template. You could then serve the first variable with the red formating and loop through the remaining values with the other formatting choice.

Use the view to generate the data you'll need in the template in the order you'd like to use it. Handle the presentation in the template.

Ed Kohler
  • 534
  • 3
  • 9