【问题标题】:django 405 method not alloweddjango 405 方法不允许
【发布时间】:2017-06-08 09:39:21
【问题描述】:

当我通过 post 方法请求时发生错误。

views.py

class ConceptForkView(ConceptBaseView, mixins.CreateModelMixin):
    print 'oclapi ConceptForkView111'

    def dispatch(self, request, *args, **kwargs):
        print 'oclapi ConceptForkView dispatch'
        return super(ConceptForkView, self).dispatch(request, *args, **kwargs)

    def post(self, request):
        print 'oclapi ConceptForkView post'

urls.py

url(r'^forking/$', ConceptForkView.as_view(), name='concept-forking'),

概念基础视图

class ConceptBaseView(ChildResourceMixin):

    lookup_field = 'concept'
    pk_field = 'mnemonic'
    model = Concept
    permission_classes = (CanViewParentDictionary,)
    child_list_attribute = 'concepts'

print 'oclapi ConceptForkView111' 命令可以运行,但是方法 dispatch 和 post 不运行。是什么原因?

我已经搜索了许多解决方案,但它们对我不起作用。我怎么解决这个问题?谢谢。

【问题讨论】:

  • 你没有提供ChildResourceMixin是什么的线索

标签: django django-rest-framework


【解决方案1】:

尝试使用

def create(request, *args, **kwargs)
    ...

方法代替

def post(self, request):
    ...

CreateModelMixin 使用 create 方法而不是 post 方法

【讨论】:

    【解决方案2】:

    请注意,mixin 不是视图。您可能还必须从 View 继承。 Mixin 通常是扩展功能的类,不是独立的。仅从 mixin 继承的类可能无法正常工作,除非这些 mixin 之一实际上不是 mixin。

    请参阅:rest framework documentationCreateAPIView 不仅继承自 CreateModelMixin,还继承自 GenericAPIView(您可能也应该继承它)。正如我们所读到的GenericAPIView

    这个类扩展了 REST 框架的 APIView 类,为标准列表和详细视图添加了通常需要的行为。

    所以这个“通常需要的行为”对于你的类表现得像视图很重要。

    【讨论】:

      猜你喜欢
      • 2019-07-05
      • 2020-07-04
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2023-03-23
      • 2011-06-21
      • 2011-04-05
      相关资源
      最近更新 更多