Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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 following url

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

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

like image 941
tuna Avatar asked Jun 18 '13 22:06

tuna


1 Answers

You have 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 didn't define the post method, it's the right behavior to get a 405 (METHOD NOT ALLOWED) error.

like image 100
Benjamin Toueg Avatar answered Sep 22 '22 13:09

Benjamin Toueg