AttributeError:'EventView'对象没有属性'get'

时间:2018-04-18 16:34:22

标签: python django

我正在处理日历应用程序。我在网页上遇到以下错误。这是我的views.py。有人请帮助我。提前致谢

views.py

from __future__ import unicode_literals
from django.shortcuts import render
from events.models import Event
import datetime
import calendar
from django.urls import reverse
from django.utils.safestring import mark_safe
from events.utils import EventCalendar
# Register your models here.

class EventView(Event):
    list_display = ['day', 'start_time', 'end_time', 'notes']
    change_list_template = 'admin/events/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:events_event_changelist') + '?day__gte=' + str(
            previous_month)
        extra_context['next_month'] = reverse('admin:events_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"')
        extra_context['calendar'] = mark_safe(html_calendar)
        return super(EventAdmin, self).changelist_view(request, extra_context)

这是我的终端

中的错误
  

Traceback(最近一次调用最后一次):文件“C:\ Users \ vikas   visking \应用程序数据\本地\程序\ Python的\ Python36 \ LIB \站点包\ Django的\核心\处理器\ exception.p   y“,第35行,在内部       response = get_response(request)文件“C:\ Users \ vikas visking \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ utils \ deprecation.py”,   li 97,致电       response = self.process_response(request,response)文件“C:\ Users \ vikas   visking \应用程序数据\本地\程序\ Python的\ Python36 \ LIB \站点包\ Django的\中间件\ clickjacking.p   y“,第26行,在process_response中       如果response.get('X-Frame-Options')不是None:AttributeError:'EventView'对象没有属性'get'[18 / Apr / 2018 21:23:56]“GET   / events / HTTP / 1.1“500 56977

2 个答案:

答案 0 :(得分:1)

尝试:

from django.views.generic import ListView
class EventView(ListView):
    model = Event

答案 1 :(得分:1)

看起来您正在尝试创建自定义模型管理员。在这种情况下,您应该是admin.ModelAdmin的子类。

from django.contrib import admin

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

admin.site.register(Event, EventAdmin)

此代码属于您的admin.py,而非您的观点。