gpt4 book ai didi

python - “函数”对象没有属性 'as_view'

转载 作者:太空狗 更新时间:2023-10-29 17:02:08 24 4
gpt4 key购买 nike

我正在尝试使用基于类的 View ,但出现了一个奇怪的错误。我使用 View 的方式似乎是正常方式:

成分/模型.py:

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 应该是一个类。所以你的 views.py 替换:

def IngredientCreateView(CreateView):

与:

class IngredientCreateView(CreateView):

关于python - “函数”对象没有属性 'as_view',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34217400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com