6
from django.views.generic import View
from django.http import HttpResponse

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

When I tried to define above method it says Method Not Allowed (GET): /

Can anyone please help me on this issue?

shiblee saidul
  • 71
  • 1
  • 1
  • 7

5 Answers5

7

In your code, you have defined post method, but no get method to handle GET request. You can put a fix like this, for example:

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

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

Check here for usage of class based view: https://docs.djangoproject.com/en/2.1/topics/class-based-views/intro/#using-class-based-views

ruddra
  • 44,912
  • 7
  • 64
  • 88
  • But when i use ur code the out put is only "get" & still i am missing the output "Class based view".Please help me to get into this matter – shiblee saidul Nov 17 '18 at 16:24
  • You can send any response you want. instead of `return HttpResponse('get')` you can put `return HttpResponse('Class based view')` in get method – ruddra Nov 17 '18 at 16:26
  • Either way, I have updated my answer. you will get response `Class based view` – ruddra Nov 17 '18 at 16:29
  • But can't i get the same output by only defining post method? – shiblee saidul Nov 17 '18 at 16:32
  • you can, but then you need to make `post` request to the view. Please check here: https://www.diffen.com/difference/GET-vs-POST-HTTP-Requests about differences between GET vs POST – ruddra Nov 17 '18 at 16:44
  • As i am new its seems difficult for me.And i am disturbing you again & again.it would be helpful for me if you illustrate it by an example.thanks.. – shiblee saidul Nov 17 '18 at 16:47
  • There are many tutorials on web regarding basic http methods. You can check this w3school tutorial: https://www.w3schools.com/tags/ref_httpmethods.asp. Also, please check here about how to make post request using postman: https://stackoverflow.com/a/9716192/2696165. FYI if this answer helped you, please [mark it as accepted](https://stackoverflow.com/help/someone-answers) – ruddra Nov 17 '18 at 16:50
5

According to View's dispatch method that you can find here:- 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)

If you don't define get method in View, then dispatch will call 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())

Here,

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

In this piece of code the if condition will pass ,but when it will try to do getattr on self, request.method.lower() have get as value, so getattr will not find get method , because we have not defined it, so getattr will return http_method_not_allowed

Ritesh Bisht
  • 162
  • 6
  • Thanks for your answer. But in base.py we define an attribute " http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'] ",where post is present .so why django is raising this error – shiblee saidul Nov 16 '18 at 06:35
  • Thanks for your answer. But in base.py we define an attribute " http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'] ",where post is present .so why django is raising this error – shiblee saidul Nov 16 '18 at 06:40
3

I can't find any 'get' method in your code. You defined only post method!.

In your View you can either define get method or call your URL with post method.

a_k_v
  • 1,388
  • 5
  • 18
2

Since you are using class based views, but you didn't specify a specific class based view template, you need to declare both the post and the get.

But if you use say TemplateView, or similar, you wont.

nadermx
  • 2,276
  • 4
  • 25
  • 56
  • let's be honest: I am flattened by how much info and clarity your simple comment is stating. Thank you. – Carlo May 03 '22 at 12:28
0

Change def post to def get, see Django docs https://docs.djangoproject.com/en/2.1/topics/class-based-views/#supporting-other-http-methods

duyue
  • 725
  • 5
  • 11