Method Not Allowed (GET): / in django












0















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?










share|improve this question



























    0















    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?










    share|improve this question

























      0












      0








      0








      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?










      share|improve this question














      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?







      python django






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 6:11









      shiblee saidulshiblee saidul

      65




      65
























          4 Answers
          4






          active

          oldest

          votes


















          1














          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






          share|improve this answer


























          • 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: diffen.com/difference/GET-vs-POST-HTTP-Requests about differences between GET vs POST

            – ruddra
            Nov 17 '18 at 16:44





















          1














          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.






          share|improve this answer































            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






            share|improve this answer































              0














              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






              share|improve this answer


























              • 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 6: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 6:40












              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332370%2fmethod-not-allowed-get-in-django%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              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






              share|improve this answer


























              • 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: diffen.com/difference/GET-vs-POST-HTTP-Requests about differences between GET vs POST

                – ruddra
                Nov 17 '18 at 16:44


















              1














              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






              share|improve this answer


























              • 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: diffen.com/difference/GET-vs-POST-HTTP-Requests about differences between GET vs POST

                – ruddra
                Nov 17 '18 at 16:44
















              1












              1








              1







              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






              share|improve this answer















              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







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 17 '18 at 16:28

























              answered Nov 16 '18 at 6:13









              ruddraruddra

              16.1k32951




              16.1k32951













              • 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: diffen.com/difference/GET-vs-POST-HTTP-Requests about differences between GET vs POST

                – ruddra
                Nov 17 '18 at 16:44





















              • 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: diffen.com/difference/GET-vs-POST-HTTP-Requests about differences between GET vs POST

                – ruddra
                Nov 17 '18 at 16:44



















              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





              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







              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





              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





              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: diffen.com/difference/GET-vs-POST-HTTP-Requests about differences between GET vs POST

              – ruddra
              Nov 17 '18 at 16:44







              you can, but then you need to make post request to the view. Please check here: diffen.com/difference/GET-vs-POST-HTTP-Requests about differences between GET vs POST

              – ruddra
              Nov 17 '18 at 16:44















              1














              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.






              share|improve this answer




























                1














                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.






                share|improve this answer


























                  1












                  1








                  1







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 6:13









                  a_k_va_k_v

                  814113




                  814113























                      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






                      share|improve this answer




























                        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






                        share|improve this answer


























                          0












                          0








                          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






                          share|improve this answer













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







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 16 '18 at 6:14









                          duyueduyue

                          47529




                          47529























                              0














                              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






                              share|improve this answer


























                              • 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 6: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 6:40
















                              0














                              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






                              share|improve this answer


























                              • 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 6: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 6:40














                              0












                              0








                              0







                              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






                              share|improve this answer















                              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







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 16 '18 at 7:06

























                              answered Nov 16 '18 at 6:29









                              Ritesh BishtRitesh Bisht

                              515




                              515













                              • 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 6: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 6:40



















                              • 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 6: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 6:40

















                              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 6: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 6: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 6:40





                              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 6:40


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332370%2fmethod-not-allowed-get-in-django%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues