Django Admin Calander无法获得下一个/上个月的反向网址

时间:2018-01-10 22:59:42

标签: django django-models django-forms django-templates django-views

我在这个反向网址调用上收到错误:

extra_context['previous_month'] = reverse('admin:portal_event_changelist') + '?day__gte=' + str(
        previous_month)

extra_context['next_month'] = reverse('admin:portal_event_changelist') + '?day__gte=' + str(next_month)

修改

这是admin.py ...我也在模板中有一个change_list.html 我在博客上发现这个calander是链接。 https://alexpnt.github.io/2017/07/15/django-calendar/

class EventAdmin(admin.ModelAdmin):
list_display = ['day', 'start_time', 'end_time', 'notes']
change_list_template = 'admin/portal/change_list.html'

def changelist_view(self, request, extra_context=None):
    after_day = request.GET.get('day__gte', None)
    extra_context = extra_context or {}

    if not after_day:
        d = datetime.date.today()
    else:
        try:
            split_after_day = after_day.split('-')
            d = datetime.date(year=int(split_after_day[0]), month=int(split_after_day[1]), day=1)
        except:
            d = datetime.date.today()

    previous_month = datetime.date(year=d.year, month=d.month, day=1)  # find first day of current month
    previous_month = previous_month - datetime.timedelta(days=1)  # backs up a single day
    previous_month = datetime.date(year=previous_month.year, month=previous_month.month,
                                   day=1)  # find first day of previous month

    last_day = calendar.monthrange(d.year, d.month)
    next_month = datetime.date(year=d.year, month=d.month, day=last_day[1])  # find last day of current month
    next_month = next_month + datetime.timedelta(days=1)  # forward a single day
    next_month = datetime.date(year=next_month.year, month=next_month.month,
                               day=1)  # find first day of next month

    extra_context['previous_month'] = reverse('admin:portal_event_changelist') + '?day__gte=' + str(
        previous_month)
    extra_context['next_month'] = reverse('admin:portal_event_changelist') + '?day__gte=' + str(next_month)

    cal = EventCalendar()
    html_calendar = cal.formatmonth(d.year, d.month, withyear=True)
    html_calendar = html_calendar.replace('<td ', '<td  width="150" height="150" style="background-color:#d2e9e6;')
    extra_context['calendar'] = mark_safe(html_calendar)
    return super(EventAdmin, self).changelist_view(request, extra_context)

admin.site.register(Event, EventAdmin)

0 个答案:

没有答案