本文介绍了405(方法不允许)用于django的ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下基于类的视图
class SupportView(BaseDetailView):
def render_to_response(self):
if self.request.method == "POST":
message = "YES"
else:
message = "NO"
return HttpResponse(message)
以下Jquery代码:
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>
以下url
url(r'^support/$', SupportView.as_view())
但是当我点击按钮我看到 127.0.0.1:8000/profiles/support/ 405(METHOD NOT ALLOWED)错误。任何想法?
But when I click the button i see 127.0.0.1:8000/profiles/support/ 405 (METHOD NOT ALLOWED) error. Any ideas ?
推荐答案
你必须实现 post 方法:
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)
由于您没有定义
post
获得 405(METHOD NOT ALLOWED)错误
的正确行为。
Since you didn't define the
post
method, it's the right behavior to get a 405 (METHOD NOT ALLOWED) error
.
这篇关于405(方法不允许)用于django的ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!