python - 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
]
解决方案
有一个错误,因为您正在使用views.index
但在您的内部views.py
,没有index
视图。所以你应该实现它,或者你可以像这样删除它:
urlpatterns = [
url(r'^calendar/$', views.CalendarView.as_view(), name='calendar'),
]
推荐阅读
- javascript - 每次在 vue 中挂载时,从 vue 组件中的 js 文件执行一个函数
- java - Excel单元格颜色不使用java程序设置实际颜色
- microsoft-graph-api - 如何使用 Microsoft Graph .NET SDK 创建和获取 Outlook 事件打开扩展数据
- jwt - 如何像 Discord 一样让 JWT 更小?
- salesforce - visualforce 页面显示未知方法错误
- javascript - 在 D3.js V5 中,带箭头的路径向后方向未正确显示
- python-3.x - 如何使用python将txt文件的行拆分为带有变量的列表
- c++ - 纹理未正确加载 OpenGL
- ubuntu - Ubuntu 上的这些 Redis 启动启动命令有什么区别?
- java - 向左/右通知交换添加事件