405 (METHOD NOT ALLOWED) for ajax request with django

I have the following class based view

class SupportView(BaseDetailView): def render_to_response(self): if self.request.method == "POST": message = "YES" else: message = "NO" return HttpResponse(message) 

And the following jQuery code:

  <script> var username = $('.username').attr('data-username'); $('.destek').click(function(){ $.ajax({ url:"/profiles/support/", type:"POST", data:{"username":username, 'csrfmiddlewaretoken': '{{csrf_token}}'}, dataType:"json" }) }) </script> 

And the following URL

  url(r'^support/$', SupportView.as_view()) 

But when I click the button, I see a 127.0.0.1:8000/profiles/support/ 405 (METHOD NOT ALLOWED) error. Any ideas ? 127.0.0.1:8000/profiles/support/ 405 (METHOD NOT ALLOWED) error. Any ideas ?

+6
source share
1 answer

You need to implement the post method in your view:

 class SupportView(BaseDetailView): def post(self, request, *args, **kwargs): self.object = self.get_object() context = self.get_context_data(object=self.object) return self.render_to_response(context) 

Since you did not define a post method, this is the correct behavior to get a 405 (METHOD NOT ALLOWED) error .

+16
source

Source: https://habr.com/ru/post/947601/


All Articles