我正在尝试使用基于课堂的视图,并获得一个奇怪的错误。我使用视图的方式似乎是正常的方式:

成分/型号。

from django.db import models
from django.utils import timezone


class Ingredient(models.Model):
    name        = models.CharField(max_length=255)
    description = models.TextField()

    def get_prices():
        purchases   = self.purchase_set.all()
        prices      = [purchase.price for purchase in purchases]

成分/Views.py:

from django.shortcuts           import render, render_to_response, redirect
from django.http                import HttpResponse, HttpResponseRedirect
from django.views.generic.edit  import CreateView
from .models                    import Ingredient, Purchase

def IngredientCreateView(CreateView):
    model = Ingredient
    fields = ['all']

成分/urls.py:

from django.conf.urls import patterns, include, url

from ingredients.views import IngredientCreateView

urlpatterns = patterns('',            
    url(r'^new_ingredient$',          IngredientCreateView.as_view(),             name='new-ingredient'),
)

我明白了

AttributeError at /ingredients/new_ingredient
'function' object has no attribute 'as_view'

我于django 1.8.5。

答案

IngredientCreateView应该是一个类。

def IngredientCreateView(CreateView):

和:

class IngredientCreateView(CreateView):

来自: stackoverflow.com