[Django]-Default to current month in MonthArchiveView

4๐Ÿ‘

โœ…

You could override the get_month and get_year methods so they return a default value:

from django.http import Http404
from django.utils.timezone import now
from django.views.generic import MonthArchiveView


class EventMonthView(MonthArchiveView):
    # ...

    def get_month(self):
        try:
            month = super(EventMonthView, self).get_month()
        except Http404:
            month = now().strftime(self.get_month_format())

        return month

    def get_year(self):
        try:
            year = super(EventMonthView, self).get_year()
        except Http404:
            year = now().strftime(self.get_year_format())

        return year
๐Ÿ‘คLuke Dixon

Leave a comment