开发者社区> 问答> 正文

为什么Python给我一个AttributeError,即使我已经定义了那个特定的类

我刚开始在Django中使用基于类的视图。当我试图启动服务器,我得到这个错误: 这是我的代码: urls . py

from . import views
from django.urls import path

app_name = 'food'

urlpatterns = [
    path('', views.IndexClassView.as_view(), name = 'index'),
    path('item/', views.item, name = 'item'),
    path('info/', views.info, name = "info"),
    path('<int:pk>/', views.FoodDetail.as_view(), name = "detail" ),
    #add form
    path('add', views.create_item, name = "create"),
    #edit item
    path('update/<int:id>/', views.update_item, name = 'update_item'),
    #delete item
    path(r'^delete/<int:id>/', views.delete_item, name = 'delete_item')
]

views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from .models import Item
from django.template import loader
from .forms import ItemForm
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from djange.views.genereic.edit import CreateView

class IndexClassView(ListView):
    model = Item
    template_name = 'food/index.html'
    context_object_name = 'item_list'

def detail(request, item_id):
    item = Item.objects.get(pk = item_id)
    context = {
        'item' : item,
    }
    return render(request, 'food/detail.html', context)

class FoodDetail(DetailView):
    model = Item
    template_name = 'food/detail.html'

...

那食物怎么可能。当视图在我眼前定义时,它没有FoodDetail属性!? 问题来源StackOverflow 地址:/questions/59381012/why-is-python-giving-me-an-attributeerror-even-if-i-have-defined-that-particular

展开
收起
kun坤 2019-12-28 14:02:26 316 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载