【问题标题】:Method Not Allowed (GET): / in django不允许的方法(GET):/在django中
【发布时间】:2019-04-19 07:38:15
【问题描述】:
from django.views.generic import View
from django.http import HttpResponse

class home(View):
  def post(self,request):
    return HttpResponse('Class based view')

当我尝试定义上述方法时,它显示 Method Not Allowed (GET): /

谁能帮我解决这个问题?

【问题讨论】:

    标签: python django


    【解决方案1】:

    由于您使用的是基于类的视图,但您没有指定特定的基于类的视图模板,因此您需要同时声明 post 和 get。

    但如果你使用说 TemplateView 或类似的,你不会。

    【讨论】:

      【解决方案2】:

      在您的代码中,您定义了post 方法,但没有get 方法来处理GET 请求。您可以像这样进行修复,例如:

      class home(View):
          def get(self, request):
             return HttpResponse('Class based view')
      
          def post(self,request):
            return HttpResponse('Class based view')
      

      在此处查看基于类的视图的用法:https://docs.djangoproject.com/en/2.1/topics/class-based-views/intro/#using-class-based-views

      【讨论】:

      • 但是当我使用你的代码时,输​​出只是“get”并且我仍然缺少输出“基于类的视图”。请帮助我解决这个问题
      • 您可以发送任何您想要的回复。而不是return HttpResponse('get') 你可以把return HttpResponse('Class based view') 放在get 方法中
      • 无论如何,我已经更新了我的答案。你会得到回复Class based view
      • 但是我不能通过只定义 post 方法得到相同的输出吗?
      • 你可以,但是你需要向视图发出post 请求。请在此处查看:diffen.com/difference/GET-vs-POST-HTTP-Requests GET 与 POST 之间的差异
      【解决方案3】:

      根据您可以在此处找到的 View 的调度方法:- https://ccbv.co.uk/projects/Django/2.0/django.views.generic.base/View/

      def dispatch(self, request, *args, **kwargs):
          # Try to dispatch to the right method; if a method doesn't exist,
          # defer to the error handler. Also defer to the error handler if the
          # request method isn't on the approved list.
          if request.method.lower() in self.http_method_names:
              handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
          else:
              handler = self.http_method_not_allowed
          return handler(request, *args, **kwargs)
      

      如果你没有在View中定义get方法,那么dispatch会调用self.http_method_not_allowed

      def http_method_not_allowed(self, request, *args, **kwargs):
          logger.warning(
              'Method Not Allowed (%s): %s', request.method, request.path,
              extra={'status_code': 405, 'request': request}
          )
          return HttpResponseNotAllowed(self._allowed_methods())
      

      这里,

      if request.method.lower() in self.http_method_names:
          handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
      

      在这段代码中,if条件会通过,但是当它尝试对self做getattr时,request.method.lower()有get作为值,所以getattr找不到get方法,因为我们没有定义它,所以 getattr 将返回 http_method_not_allowed

      【讨论】:

      • 感谢您的回答。但是在 base.py 中,我们定义了一个属性“http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'] ”,其中 post 是现在 .so 为什么 django 会引发这个错误
      • 感谢您的回答。但是在 base.py 中,我们定义了一个属性“http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'] ”,其中 post 是现在 .so 为什么 django 会引发这个错误
      【解决方案4】:

      【讨论】:

        【解决方案5】:

        我在您的代码中找不到任何“获取”方法。您只定义了 post 方法!。

        在您的View 中,您可以定义 get 方法或使用 post 方法调用您的 URL。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-06-27
          • 2021-07-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-09
          • 1970-01-01
          • 2021-02-18
          • 1970-01-01
          相关资源
          最近更新 更多