首页 > 解决方案 > Django AttributeError:模块'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
]

标签: pythondjango

解决方案


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

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

推荐阅读