Как вывести несколько встроенных модулей HTMLCalendar в Django

На самом деле, я не до конца понял, как работает HTMLCalendar, встроенный модуль, предоставляемый Django. Я пытаюсь распечатать два Календаря на одной странице, с обеих сторон. В каждом из двух календарей выводятся разные события.

Как создать два календаря, которые имеют одну и ту же функцию, но выводят разные события? Прежде всего, приведенный ниже код является модифицированным кодом. Но он выдает ошибку.

Я пытался напечатать два календаря, но печатается четыре и ('\n\n\n\n\n\n\n\n\n\n', ' печатается между таблицами календаря.

[event.html]

<head>
  {% block title %}
    <title>Calendar</title>
  {% endblock %}
</head>

<body>
    {% block content %}
        <div class="row">
            <h4>Calendar</h4>
              <span style="font-size:0.85em; float:right;">
              <div style="display:inline-block;"></div> event1 &nbsp;
              <div style="display:inline-block;"></div> event2 &nbsp;
              <div style="display:inline-block;"></div> event3 &nbsp;</span>
        </div>

        <div>
          <br>
            <a class="btn btn-info" href="{% url 'leave:calendar' %}?{{ prev_month }}">Previous</a>
            <div style="float:right;">
              <a class="btn btn-info" href="/leave/calendar/new/">Add</a>
              <a class="btn btn-info" href="{% url 'leave:calendar' %}?{{ next_month }}">Next</a>
            </div>
        </div>

        <div>
          {{calendar}}
        </div>
        <div>
          {{calendar_2}} ---> 'ADD'
        </div>
      {% endblock %}
</body>

[utils.py]

from calendar import HTMLCalendar
from .models import Leave
from django.db.models import Q, F
from django.db.models.functions import ExtractMonth

class Calendar(HTMLCalendar):
    def __init__(self, year=None, month=None, user=None):
        self.year = year
        self.month = month
        self.user = user
        super(Calendar, self).__init__()

    # formats a day as a td
    # filter events by day
    def formatday(self, day, events, next_visit, cycle_visit):
    ....

    # formats a week as a tr
    def formatweek(self, theweek, events, next_visit, cycle_visit):
    ....

    # formats a month as a table
    # filter events by year and month
    def formatmonth(self, withyear=True):
    events = Leave.objects.filter(Q(from_date__year=self.year, from_date__month=self.month) |
                                  Q(end_date__year=self.year, end_date__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'
    cal_2 = f'<table border="0" cellpadding="0" cellspacing="0" class="calendar">\n'  -- 'ADD'
    cal_2 += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}\n'    -- 'ADD'
    cal_2 += f'{self.formatweekheader()}\n'                                           
    for week in self.monthdays2calendar(self.year, self.month):
        cal += f'{self.formatweek(week, events, next_visit, cycle_visit)}\n'
        cal_2 += f'{self.formatweek(week, events, next_visit, cycle_visit)}\n'       -- 'ADD'
    return cal, cal_2  -- 'ADD'

[views.py]

class CalendarView(generic.ListView):
    model = Leave
    template_name = 'pages/leave/leave.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        # use today's date for the calendar
        d = get_date(self.request.GET.get('month', None))
        context['prev_month'] = prev_month(d)
        context['next_month'] = next_month(d)
        first_name = self.request.user

        # Instantiate our calendar class with today's year and date
        cal = Calendar(d.year, d.month, first_name)
        cal.setfirstweekday(6)

    # Call the formatmonth method, which returns our calendar as a table
    html_cal = cal.formatmonth(withyear=True)
    context['calendar'] = mark_safe(html_cal)
    context['calendar_2'] = mark_safe(html_cal)   --- 'ADD'
    context['leave'] = Leave.objects.filter(is_deleted=0)

    return context
Вернуться на верх