You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django日历应用报错:'EventView'对象无'get'属性求协助

Fixing the AttributeError: 'EventView' object has no attribute 'get'

Hey there! Let's break down what's going wrong here and fix that error.

The Root Cause

Looking at your views.py snippet, your EventView class is inheriting from Event—which is a model class, not a Django view base class. Model classes are designed for database interactions, not handling HTTP requests like GET or POST. That's why you're getting the error: the Event model doesn't have a get() method, which Django expects from a view to handle incoming requests.

How to Fix It

You need to have EventView inherit from one of Django's view classes instead. Depending on what you want this view to do, here are two common options:

Option 1: Use a Generic View (e.g., ListView)

If your goal is to display a list of events alongside your calendar, ListView is perfect—it handles most of the heavy lifting for you:

from __future__ import unicode_literals
from django.shortcuts import render
from django.views.generic import ListView  # Add this import
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

class EventView(ListView):
    model = Event
    template_name = 'events/event_calendar.html'  # Replace with your actual template path
    context_object_name = 'events'  # Name to reference events in your template

    # Add custom context (like your calendar) by overriding this method
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        today = datetime.date.today()
        cal = EventCalendar()
        context['calendar'] = mark_safe(cal.formatmonth(today.year, today.month))
        return context

Option 2: Use a Basic View Class (Full Control)

If you need total control over request handling, inherit from View and implement the get() method yourself:

from __future__ import unicode_literals
from django.shortcuts import render
from django.views import View  # Add this import
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

class EventView(View):
    def get(self, request, *args, **kwargs):
        # Your custom logic here—fetch events, generate calendar, etc.
        today = datetime.date.today()
        events = Event.objects.all()
        cal = EventCalendar()
        calendar_html = mark_safe(cal.formatmonth(today.year, today.month))

        return render(request, 'events/event_calendar.html', {
            'events': events,
            'calendar': calendar_html
        })

Key Takeaway

Django views rely on inheriting from Django's built-in view base classes (View, ListView, DetailView, etc.) to access request-handling methods like get(). Model classes are for defining your database structure—they don't know how to process web requests.

内容的提问来源于stack exchange,提问作者Vīkāś Vīskīñg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:45:39