【问题标题】:'function' object has no attribute 'as_view''function' 对象没有属性 'as_view'
【发布时间】:2016-03-17 00:05:48
【问题描述】:

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

成分/models.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 上。为什么这种观点不起作用?谢谢

【问题讨论】:

  • 在我的例子中,我给viewsetmodel 取了相同的名字。给他们起不同的名字解决了我的问题

标签: python django django-class-based-views


【解决方案1】:

就我而言,问题在于我试图在基于类的视图上使用@decorator,就好像它是基于函数的视图一样,而不是@decorating the class correctly

编辑:从链接页面,这是一种将@login_required 应用于基于类的视图的方法:

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator

@method_decorator(login_required, name='dispatch')
class ProtectedView(TemplateView):

【讨论】:

  • 谢谢我实际上犯了@csrf_exept装饰器的错误,因此它失败了
  • 请问name='dispatch'是什么?你什么意思
  • @KokHyvv 装饰类时,实际上是在装饰类中的一个函数,在本例中是运行视图的“调度”函数。由于 dispatch 函数是继承而来的,所以它的名字只出现在装饰器中。
  • 啊哈,我明白了,谢谢你的解释
【解决方案2】:
def Some_def_View(CreateView):

#应该替换为

class SomeClassView(CreateView)

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,但这个解决方案对我有用..

    在视图类的 views.py 文件中,您可以使用视图集而不是 CreateView

                from rest_framework import viewsets
                class YourClassView(viewsets.ModelViewSet):
    

    urls.py 文件中你可以使用这个路由模式

              from django.conf.urls import url
              from rest_framework import routers
    
              router = routers.DefaultRouter()
              router.register('books',YourClassView)
    
              urlpatterns = [
                   path('', include(router.urls)),
                   path('admin/', admin.site.urls)
                ]
    

    【讨论】:

      【解决方案4】:

      除了这里已经说过的,检查文件名和类名是否相同,那么你可能需要正确导入类。

      File name /api/A.py
      
      class A:
      //some methods
      

      在你的主课中

      //App main class
      from api.A import A
      

      【讨论】:

        【解决方案5】:

        IngredientCreateView 是一个函数,而不是一个类。

        下面一行

        def IngredientCreateView(CreateView):
        

        应该替换为

        class IngredientCreateView(CreateView):
        

        【讨论】:

        • idk 谁是第一个,但谢谢。旧习惯难改,要习惯课堂观点:)
        【解决方案6】:

        IngredientCreateView 应该是一个类。 所以你的views.py替换:

        def IngredientCreateView(CreateView):
        

        与:

        class IngredientCreateView(CreateView):
        

        【讨论】:

        • 就我而言,我给viewsetmodel 取了相同的名字。给他们起不同的名字解决了我的问题
        • 我一直盯着文件看太久了,我什至没有看到。 ??
        猜你喜欢
        • 2021-01-12
        • 2016-12-08
        • 2021-04-24
        • 2022-06-13
        • 2011-10-13
        • 2020-05-21
        • 2019-08-13
        • 2017-08-02
        • 1970-01-01
        相关资源
        最近更新 更多