Django 属性错误:模块'cal.views'没有属性'index'



我使用 django 创建日历时遇到问题,这段代码类似于教程,但问题出在文件夹 cal/views 我使用 Django 的代码检测到属性错误,我不再知道可能出现什么问题,我已经检查了"cal"文件夹中的文件 请帮助我编写代码:(

这是 descubretepic/cal/views 中的代码

from datetime import datetime
from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
from django.utils.safestring import mark_safe
from .models import *
from .utils import Calendar
class CalendarView(generic.ListView):
model = Event
template_name = 'cal/calendar.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('day', None))
# Instantiate our calendar class with today's year and date
cal = Calendar(d.year, d.month)
# Call the formatmonth method, which returns our calendar as a table
html_cal = cal.formatmonth(withyear=True)
context['calendar'] = mark_safe(html_cal)
return context
def get_date(req_day):
if req_day:
year, month = (int(x) for x in req_day.split('-'))
return date(year, month, day=1)
return datetime.today()

我在 descubretepic/cal/urls 中的代码

from django.conf.urls import url
from . import views

app_name = 'cal'
urlpatterns = [
'',
url(r'^$', views.index, name='index'),
url(r'^calendar/$', views.CalendarView.as_view(), name='calendar'), # here
]

有一个错误,因为您正在使用views.index但在views.py中,没有index视图。所以你应该实现它,或者你可以像这样删除它:

urlpatterns = [
url(r'^calendar/$', views.CalendarView.as_view(), name='calendar'),
]

最新更新